mirror of https://github.com/velour/catbase.git
Initial (ugly) web interface
This commit is contained in:
parent
45b6ffa819
commit
edd941fe46
21
bot/bot.go
21
bot/bot.go
|
@ -1,9 +1,11 @@
|
||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/chrissexton/alepale/config"
|
"github.com/chrissexton/alepale/config"
|
||||||
irc "github.com/fluffle/goirc/client"
|
irc "github.com/fluffle/goirc/client"
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -35,6 +37,9 @@ type Bot struct {
|
||||||
logOut chan Messages
|
logOut chan Messages
|
||||||
|
|
||||||
Version string
|
Version string
|
||||||
|
|
||||||
|
// The entries to the bot's HTTP interface
|
||||||
|
httpEndPoints []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log provides a slice of messages in order
|
// 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,
|
Config: config,
|
||||||
Plugins: make(map[string]Handler),
|
Plugins: make(map[string]Handler),
|
||||||
PluginOrdering: make([]string, 0),
|
PluginOrdering: make([]string, 0),
|
||||||
|
@ -140,12 +145,20 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
|
||||||
logOut: logOut,
|
logOut: logOut,
|
||||||
Version: config.Version,
|
Version: config.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
http.HandleFunc("/", bot.serveRoot)
|
||||||
|
go http.ListenAndServe(":8080", nil)
|
||||||
|
|
||||||
|
return bot
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a constructed handler to the bots handlers list
|
// Adds a constructed handler to the bots handlers list
|
||||||
func (b *Bot) AddHandler(name string, h Handler) {
|
func (b *Bot) AddHandler(name string, h Handler) {
|
||||||
b.Plugins[strings.ToLower(name)] = h
|
b.Plugins[strings.ToLower(name)] = h
|
||||||
b.PluginOrdering = append(b.PluginOrdering, name)
|
b.PluginOrdering = append(b.PluginOrdering, name)
|
||||||
|
if entry := h.RegisterWeb(); entry != nil {
|
||||||
|
b.httpEndPoints = append(b.httpEndPoints, *entry)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends message to channel
|
// Sends message to channel
|
||||||
|
@ -185,3 +198,9 @@ RET:
|
||||||
b.logIn <- msg
|
b.logIn <- msg
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ type Handler interface {
|
||||||
Event(kind string, message Message) bool
|
Event(kind string, message Message) bool
|
||||||
BotMessage(message Message) bool
|
BotMessage(message Message) bool
|
||||||
Help(channel string, parts []string)
|
Help(channel string, parts []string)
|
||||||
|
RegisterWeb() *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks to see if our user exists and if any changes have occured to it
|
// Checks to see if our user exists and if any changes have occured to it
|
||||||
|
|
|
@ -96,3 +96,8 @@ func (p *AdminPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *AdminPlugin) BotMessage(message bot.Message) bool {
|
func (p *AdminPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *AdminPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -374,3 +374,8 @@ func (p *BeersPlugin) checkUntappd(channel string) {
|
||||||
func (p *BeersPlugin) BotMessage(message bot.Message) bool {
|
func (p *BeersPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *BeersPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -188,3 +188,8 @@ func (p *CounterPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *CounterPlugin) BotMessage(message bot.Message) bool {
|
func (p *CounterPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *CounterPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -98,3 +98,8 @@ func (p *DicePlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *DicePlugin) BotMessage(message bot.Message) bool {
|
func (p *DicePlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *DicePlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -153,3 +153,8 @@ func (p *DowntimePlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *DowntimePlugin) BotMessage(message bot.Message) bool {
|
func (p *DowntimePlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *DowntimePlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -3,9 +3,12 @@ package plugins
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/chrissexton/alepale/bot"
|
"github.com/chrissexton/alepale/bot"
|
||||||
|
"html/template"
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -442,3 +445,37 @@ func (p *FactoidPlugin) factTimer(channel string) {
|
||||||
func (p *FactoidPlugin) BotMessage(message bot.Message) bool {
|
func (p *FactoidPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -146,3 +146,8 @@ func (p *FirstPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *FirstPlugin) BotMessage(message bot.Message) bool {
|
func (p *FirstPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *FirstPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ type Plugin interface {
|
||||||
BotMessage(message bot.Message) bool
|
BotMessage(message bot.Message) bool
|
||||||
LoadData()
|
LoadData()
|
||||||
Help()
|
Help()
|
||||||
|
RegisterWeb()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Below are some example plugins
|
// ---- Below are some example plugins
|
||||||
|
|
|
@ -180,3 +180,8 @@ func (p *RememberPlugin) BotMessage(message bot.Message) bool {
|
||||||
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *RememberPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -44,3 +44,8 @@ func (p *SkeletonPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *SkeletonPlugin) BotMessage(message bot.Message) bool {
|
func (p *SkeletonPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *SkeletonPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -110,3 +110,8 @@ func (p *TalkerPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *TalkerPlugin) BotMessage(message bot.Message) bool {
|
func (p *TalkerPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *TalkerPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -134,3 +134,8 @@ func (p *TwitterPlugin) checkMessages() {
|
||||||
func (p *TwitterPlugin) BotMessage(message bot.Message) bool {
|
func (p *TwitterPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *TwitterPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
`
|
|
@ -57,3 +57,8 @@ func (p *YourPlugin) Event(kind string, message bot.Message) bool {
|
||||||
func (p *YourPlugin) BotMessage(message bot.Message) bool {
|
func (p *YourPlugin) BotMessage(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register any web URLs desired
|
||||||
|
func (p *YourPlugin) RegisterWeb() *string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue