2013-01-25 21:33:41 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-05-08 00:08:18 +00:00
|
|
|
"github.com/chrissexton/alepale/bot"
|
2013-01-25 21:33:41 +00:00
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is a first plugin to serve as an example and quick copy/paste for new plugins.
|
|
|
|
|
|
|
|
type FirstPlugin struct {
|
|
|
|
First *FirstEntry
|
|
|
|
Bot *bot.Bot
|
|
|
|
Coll *mgo.Collection
|
|
|
|
}
|
|
|
|
|
|
|
|
type FirstEntry struct {
|
|
|
|
Day time.Time
|
|
|
|
Time time.Time
|
|
|
|
Body string
|
|
|
|
Nick string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface
|
|
|
|
func NewFirstPlugin(b *bot.Bot) *FirstPlugin {
|
|
|
|
coll := b.Db.C("first")
|
|
|
|
var firsts []FirstEntry
|
2013-06-02 01:58:20 +00:00
|
|
|
query := bson.M{"day": midnight(time.Now())}
|
|
|
|
log.Println("Day:", midnight(time.Now()))
|
2013-01-25 21:33:41 +00:00
|
|
|
coll.Find(query).All(&firsts)
|
|
|
|
|
2013-06-02 01:58:20 +00:00
|
|
|
log.Println("FIRSTS:", firsts)
|
|
|
|
|
2013-01-25 21:36:19 +00:00
|
|
|
var first *FirstEntry
|
|
|
|
if len(firsts) > 0 {
|
|
|
|
first = &firsts[0]
|
|
|
|
}
|
|
|
|
|
2013-01-25 21:33:41 +00:00
|
|
|
return &FirstPlugin{
|
|
|
|
Bot: b,
|
|
|
|
Coll: coll,
|
2013-01-25 21:36:19 +00:00
|
|
|
First: first,
|
2013-01-25 21:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func midnight(t time.Time) time.Time {
|
|
|
|
y, m, d := t.Date()
|
|
|
|
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isToday(t time.Time) bool {
|
|
|
|
t0 := midnight(t)
|
|
|
|
return t0.Before(midnight(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message responds to the bot hook on recieving messages.
|
|
|
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
|
|
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
|
|
|
func (p *FirstPlugin) Message(message bot.Message) bool {
|
|
|
|
// This bot does not reply to anything
|
|
|
|
|
2013-03-30 11:12:27 +00:00
|
|
|
if p.First == nil && p.allowed(message) {
|
2013-01-25 21:33:41 +00:00
|
|
|
p.recordFirst(message)
|
2013-03-30 11:12:27 +00:00
|
|
|
return false
|
|
|
|
} else if p.First != nil {
|
|
|
|
if isToday(p.First.Time) && p.allowed(message) {
|
2013-01-25 21:33:41 +00:00
|
|
|
p.recordFirst(message)
|
2013-02-10 15:08:01 +00:00
|
|
|
return false
|
2013-01-25 21:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := strings.NewReplacer("'", "", "\"", "", ",", "", ".", "", ":", "",
|
|
|
|
"?", "", "!", "")
|
|
|
|
msg := strings.ToLower(message.Body)
|
|
|
|
if r.Replace(msg) == "whos on first" {
|
2013-02-01 13:41:18 +00:00
|
|
|
p.announceFirst(message)
|
2013-03-30 11:12:27 +00:00
|
|
|
log.Printf("Disallowing %s: %s from first.",
|
|
|
|
message.User.Name, message.Body)
|
2013-02-01 13:41:18 +00:00
|
|
|
return true
|
2013-01-25 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-03-30 11:12:27 +00:00
|
|
|
func (p *FirstPlugin) allowed(message bot.Message) bool {
|
2013-04-21 16:37:04 +00:00
|
|
|
for _, msg := range p.Bot.Config.Bad.Msgs {
|
2013-03-30 11:12:27 +00:00
|
|
|
if strings.ToLower(msg) == strings.ToLower(message.Body) {
|
|
|
|
log.Println("Disallowing first: ", message.User.Name, ":", message.Body)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 20:49:00 +00:00
|
|
|
for _, host := range p.Bot.Config.Bad.Hosts {
|
|
|
|
if host == message.Host {
|
|
|
|
log.Println("Disallowing first: ", message.User.Name, ":", message.Body)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, nick := range p.Bot.Config.Bad.Nicks {
|
|
|
|
if nick == message.User.Name {
|
|
|
|
log.Println("Disallowing first: ", message.User.Name, ":", message.Body)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 11:12:27 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-01-25 21:33:41 +00:00
|
|
|
func (p *FirstPlugin) recordFirst(message bot.Message) {
|
|
|
|
log.Println("Recording first: ", message.User.Name, ":", message.Body)
|
|
|
|
p.First = &FirstEntry{
|
|
|
|
Day: midnight(time.Now()),
|
|
|
|
Time: message.Time,
|
|
|
|
Body: message.Body,
|
|
|
|
Nick: message.User.Name,
|
|
|
|
}
|
|
|
|
p.Coll.Insert(p.First)
|
2013-02-01 13:41:18 +00:00
|
|
|
p.announceFirst(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FirstPlugin) announceFirst(message bot.Message) {
|
|
|
|
c := message.Channel
|
|
|
|
if p.First != nil {
|
|
|
|
p.Bot.SendMessage(c, fmt.Sprintf("%s had first at %s with the message: \"%s\"",
|
|
|
|
p.First.Nick, p.First.Time.Format(time.Kitchen), p.First.Body))
|
|
|
|
}
|
2013-01-25 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadData imports any configuration data into the plugin. This is not strictly necessary other
|
|
|
|
// than the fact that the Plugin interface demands it exist. This may be deprecated at a later
|
|
|
|
// date.
|
|
|
|
func (p *FirstPlugin) LoadData() {
|
|
|
|
// This bot has no data to load
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *FirstPlugin) Help(channel string, parts []string) {
|
|
|
|
p.Bot.SendMessage(channel, "Sorry, First does not do a goddamn thing.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
|
|
|
func (p *FirstPlugin) Event(kind string, message bot.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Handler for bot's own messages
|
|
|
|
func (p *FirstPlugin) BotMessage(message bot.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *FirstPlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|