From edd941fe46155157714ffd74b18baff59b79adb5 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 1 Jun 2013 13:10:15 -0400 Subject: [PATCH] Initial (ugly) web interface --- bot/bot.go | 21 ++++++++++++++++++++- bot/handlers.go | 1 + plugins/admin.go | 5 +++++ plugins/beers.go | 5 +++++ plugins/counter.go | 5 +++++ plugins/dice.go | 5 +++++ plugins/downtime.go | 5 +++++ plugins/factoid.go | 37 +++++++++++++++++++++++++++++++++++++ plugins/first.go | 5 +++++ plugins/plugins.go | 1 + plugins/remember.go | 5 +++++ plugins/skeleton.go | 5 +++++ plugins/talker.go | 5 +++++ plugins/twitter.go | 5 +++++ plugins/webTemplates.go | 36 ++++++++++++++++++++++++++++++++++++ plugins/your.go | 5 +++++ 16 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 plugins/webTemplates.go diff --git a/bot/bot.go b/bot/bot.go index b35a021..2e665da 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -1,9 +1,11 @@ package bot import ( + "fmt" "github.com/chrissexton/alepale/config" irc "github.com/fluffle/goirc/client" "labix.org/v2/mgo" + "net/http" "strings" "time" ) @@ -35,6 +37,9 @@ type Bot struct { logOut chan Messages Version string + + // The entries to the bot's HTTP interface + httpEndPoints []string } // Log provides a slice of messages in order @@ -126,7 +131,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { }, } - return &Bot{ + bot := &Bot{ Config: config, Plugins: make(map[string]Handler), PluginOrdering: make([]string, 0), @@ -140,12 +145,20 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { logOut: logOut, Version: config.Version, } + + http.HandleFunc("/", bot.serveRoot) + go http.ListenAndServe(":8080", nil) + + return bot } // Adds a constructed handler to the bots handlers list func (b *Bot) AddHandler(name string, h Handler) { b.Plugins[strings.ToLower(name)] = h b.PluginOrdering = append(b.PluginOrdering, name) + if entry := h.RegisterWeb(); entry != nil { + b.httpEndPoints = append(b.httpEndPoints, *entry) + } } // Sends message to channel @@ -185,3 +198,9 @@ RET: b.logIn <- msg return } + +func (b *Bot) serveRoot(w http.ResponseWriter, r *http.Request) { + for _, entry := range b.httpEndPoints { + fmt.Fprintf(w, "%s", entry, entry) + } +} diff --git a/bot/handlers.go b/bot/handlers.go index 8a78faf..0dab231 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -18,6 +18,7 @@ type Handler interface { Event(kind string, message Message) bool BotMessage(message Message) bool Help(channel string, parts []string) + RegisterWeb() *string } // Checks to see if our user exists and if any changes have occured to it diff --git a/plugins/admin.go b/plugins/admin.go index 6e39010..0047f6a 100644 --- a/plugins/admin.go +++ b/plugins/admin.go @@ -96,3 +96,8 @@ func (p *AdminPlugin) Event(kind string, message bot.Message) bool { func (p *AdminPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *AdminPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/beers.go b/plugins/beers.go index f390c2f..e7ab4ce 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -374,3 +374,8 @@ func (p *BeersPlugin) checkUntappd(channel string) { func (p *BeersPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *BeersPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/counter.go b/plugins/counter.go index 566e808..53878dc 100644 --- a/plugins/counter.go +++ b/plugins/counter.go @@ -188,3 +188,8 @@ func (p *CounterPlugin) Event(kind string, message bot.Message) bool { func (p *CounterPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *CounterPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/dice.go b/plugins/dice.go index 602e489..5617e26 100644 --- a/plugins/dice.go +++ b/plugins/dice.go @@ -98,3 +98,8 @@ func (p *DicePlugin) Event(kind string, message bot.Message) bool { func (p *DicePlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *DicePlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/downtime.go b/plugins/downtime.go index 5572669..97679b7 100644 --- a/plugins/downtime.go +++ b/plugins/downtime.go @@ -153,3 +153,8 @@ func (p *DowntimePlugin) Event(kind string, message bot.Message) bool { func (p *DowntimePlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *DowntimePlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/factoid.go b/plugins/factoid.go index 0af1638..1cda8b6 100644 --- a/plugins/factoid.go +++ b/plugins/factoid.go @@ -3,9 +3,12 @@ package plugins import ( "fmt" "github.com/chrissexton/alepale/bot" + "html/template" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" + "log" "math/rand" + "net/http" "regexp" "strings" "time" @@ -442,3 +445,37 @@ func (p *FactoidPlugin) factTimer(channel string) { func (p *FactoidPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *FactoidPlugin) RegisterWeb() *string { + http.HandleFunc("/factoid/req", p.serveQuery) + http.HandleFunc("/factoid", p.serveIndex) + tmp := "/factoid" + return &tmp +} + +// Serve up the index template +func (p *FactoidPlugin) serveIndex(w http.ResponseWriter, r *http.Request) { + t, err := template.New("factoidIndex").Parse(factoidIndex) + if err != nil { + log.Println(err) + } + t.Execute(w, nil) +} + +func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { + context := make(map[string]interface{}) + if e := r.PostFormValue("entry"); e != "" { + var entries []Factoid + p.Coll.Find(bson.M{"trigger": e}).All(&entries) + context["Count"] = fmt.Sprintf("Found %d entries", len(entries)) + context["Entries"] = entries + } else { + context["Error"] = "Something's fucked." + } + t, err := template.New("factoidIndex").Parse(factoidIndex) + if err != nil { + log.Println(err) + } + t.Execute(w, context) +} diff --git a/plugins/first.go b/plugins/first.go index b150a85..9520ccf 100644 --- a/plugins/first.go +++ b/plugins/first.go @@ -146,3 +146,8 @@ func (p *FirstPlugin) Event(kind string, message bot.Message) bool { func (p *FirstPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *FirstPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/plugins.go b/plugins/plugins.go index 4cd8e28..991def9 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -10,6 +10,7 @@ type Plugin interface { BotMessage(message bot.Message) bool LoadData() Help() + RegisterWeb() } // ---- Below are some example plugins diff --git a/plugins/remember.go b/plugins/remember.go index 0dfd8d4..04ab5c8 100644 --- a/plugins/remember.go +++ b/plugins/remember.go @@ -180,3 +180,8 @@ func (p *RememberPlugin) BotMessage(message bot.Message) bool { p.Log[message.Channel] = append(p.Log[message.Channel], message) return false } + +// Register any web URLs desired +func (p *RememberPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/skeleton.go b/plugins/skeleton.go index a3ab3f4..aff1caf 100644 --- a/plugins/skeleton.go +++ b/plugins/skeleton.go @@ -44,3 +44,8 @@ func (p *SkeletonPlugin) Event(kind string, message bot.Message) bool { func (p *SkeletonPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *SkeletonPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/talker.go b/plugins/talker.go index 0070bfb..0871ad2 100644 --- a/plugins/talker.go +++ b/plugins/talker.go @@ -110,3 +110,8 @@ func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { func (p *TalkerPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *TalkerPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/twitter.go b/plugins/twitter.go index d0a71f5..a981eb2 100644 --- a/plugins/twitter.go +++ b/plugins/twitter.go @@ -134,3 +134,8 @@ func (p *TwitterPlugin) checkMessages() { func (p *TwitterPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *TwitterPlugin) RegisterWeb() *string { + return nil +} diff --git a/plugins/webTemplates.go b/plugins/webTemplates.go new file mode 100644 index 0000000..1990c28 --- /dev/null +++ b/plugins/webTemplates.go @@ -0,0 +1,36 @@ +package plugins + +// I hate this, but I'm creating strings of the templates to avoid having to +// track where templates reside. + +var factoidIndex string = ` + + + +Factoids + + + {{if .Error}} +
{{.Error}}
+ {{end}} + +
+
+ +
+
+ + {{ $entries := .Entries }} + + {{if .Count}} +
Found {{.Count}} entries.
+ {{end}} + + {{range $entries}} +
+ {{.Trigger}} - {{.Action}} +
+ {{end}} + + +` diff --git a/plugins/your.go b/plugins/your.go index fc5ba71..171ed3a 100644 --- a/plugins/your.go +++ b/plugins/your.go @@ -57,3 +57,8 @@ func (p *YourPlugin) Event(kind string, message bot.Message) bool { func (p *YourPlugin) BotMessage(message bot.Message) bool { return false } + +// Register any web URLs desired +func (p *YourPlugin) RegisterWeb() *string { + return nil +}