Initial (ugly) web interface

This commit is contained in:
Chris Sexton 2013-06-01 13:10:15 -04:00
parent 45b6ffa819
commit edd941fe46
16 changed files with 150 additions and 1 deletions

View File

@ -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, "<a href=\"%s\">%s</a>", entry, entry)
}
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -10,6 +10,7 @@ type Plugin interface {
BotMessage(message bot.Message) bool
LoadData()
Help()
RegisterWeb()
}
// ---- Below are some example plugins

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

36
plugins/webTemplates.go Normal file
View File

@ -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 = `
<!DOCTYPE html>
<html>
<head>
<title>Factoids</title>
</head>
{{if .Error}}
<div id="error">{{.Error}}</div>
{{end}}
<div>
<form action="/factoid/req" method="POST">
<input type="text" name="entry" /> <input type="submit" value="Find" />
</form>
</div>
{{ $entries := .Entries }}
{{if .Count}}
<div id="count">Found {{.Count}} entries.</div>
{{end}}
{{range $entries}}
<div class="entry">
{{.Trigger}} - {{.Action}}
</div>
{{end}}
</html>
`

View File

@ -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
}