2013-12-10 23:37:07 +00:00
|
|
|
// © 2013 the AlePale Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
2012-08-20 01:28:25 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-01 19:00:57 +00:00
|
|
|
"log"
|
2012-08-20 01:28:25 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-01-15 06:12:26 +00:00
|
|
|
|
|
|
|
"github.com/chrissexton/alepale/bot"
|
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
2012-08-20 01:28:25 +00:00
|
|
|
)
|
|
|
|
|
2013-05-07 23:28:34 +00:00
|
|
|
// This is a skeleton plugin to serve as an example and quick copy/paste for new
|
|
|
|
// plugins.
|
2012-08-20 01:28:25 +00:00
|
|
|
|
|
|
|
type RememberPlugin struct {
|
|
|
|
Bot *bot.Bot
|
|
|
|
Coll *mgo.Collection
|
|
|
|
Log map[string][]bot.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRememberPlugin creates a new RememberPlugin with the Plugin interface
|
|
|
|
func NewRememberPlugin(b *bot.Bot) *RememberPlugin {
|
|
|
|
p := RememberPlugin{
|
|
|
|
Bot: b,
|
|
|
|
Log: make(map[string][]bot.Message),
|
|
|
|
}
|
|
|
|
p.LoadData()
|
2012-08-27 15:34:21 +00:00
|
|
|
// for _, channel := range b.Config.Channels {
|
|
|
|
// go p.quoteTimer(channel)
|
|
|
|
// }
|
2012-08-20 01:28:25 +00:00
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message responds to the bot hook on recieving messages.
|
2013-05-07 23:28:34 +00:00
|
|
|
// 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.
|
2012-08-20 01:28:25 +00:00
|
|
|
func (p *RememberPlugin) Message(message bot.Message) bool {
|
|
|
|
|
2013-09-01 02:24:10 +00:00
|
|
|
if strings.ToLower(message.Body) == "quote" && message.Command {
|
2012-08-20 01:28:25 +00:00
|
|
|
q := p.randQuote()
|
|
|
|
p.Bot.SendMessage(message.Channel, q)
|
2012-08-23 20:28:45 +00:00
|
|
|
|
|
|
|
// is it evil not to remember that the user said quote?
|
2012-08-20 01:28:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-08-27 15:34:21 +00:00
|
|
|
user := message.User
|
2013-06-17 03:44:24 +00:00
|
|
|
parts := strings.Split(message.Body, " ")
|
2013-09-01 02:24:10 +00:00
|
|
|
if message.Command && len(parts) >= 3 &&
|
|
|
|
strings.ToLower(parts[0]) == "remember" {
|
|
|
|
|
2012-08-20 01:28:25 +00:00
|
|
|
// we have a remember!
|
2013-05-07 23:31:16 +00:00
|
|
|
// look through the logs and find parts[1] as a user, if not,
|
|
|
|
// fuck this hoser
|
2013-09-01 03:05:00 +00:00
|
|
|
snips := strings.Split(strings.Join(parts[1:], " "), "$and")
|
|
|
|
var msgs []string
|
|
|
|
var trigger string
|
|
|
|
|
|
|
|
for _, snip := range snips {
|
2013-09-01 03:33:15 +00:00
|
|
|
snip = strings.TrimSpace(snip)
|
2013-09-01 03:05:00 +00:00
|
|
|
snipParts := strings.Split(snip, " ")
|
|
|
|
nick := snipParts[0]
|
|
|
|
snip := strings.Join(snipParts[1:], " ")
|
|
|
|
|
|
|
|
for i := len(p.Log[message.Channel]) - 1; i >= 0; i-- {
|
|
|
|
entry := p.Log[message.Channel][i]
|
|
|
|
|
|
|
|
if strings.ToLower(entry.User.Name) == strings.ToLower(nick) &&
|
|
|
|
strings.Contains(
|
|
|
|
strings.ToLower(entry.Body),
|
|
|
|
strings.ToLower(snip),
|
|
|
|
) {
|
|
|
|
|
|
|
|
// check if it's an action
|
|
|
|
if entry.Action {
|
|
|
|
msgs = append(msgs, fmt.Sprintf("*%s* %s", entry.User.Name, entry.Body))
|
|
|
|
} else {
|
|
|
|
msgs = append(msgs, fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body))
|
|
|
|
}
|
|
|
|
|
|
|
|
if trigger == "" {
|
|
|
|
trigger = fmt.Sprintf("%s quotes", entry.User.Name)
|
|
|
|
}
|
2013-05-07 23:31:16 +00:00
|
|
|
|
2013-06-01 19:00:57 +00:00
|
|
|
}
|
2013-09-01 03:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msgs) == len(snips) {
|
|
|
|
msg := strings.Join(msgs, "$and")
|
2016-01-15 06:12:26 +00:00
|
|
|
// Needs to be upgraded to SQL
|
|
|
|
// err := p.Bot.Db.Run(
|
|
|
|
// bson.M{"eval": "return counter(\"factoid\");"},
|
|
|
|
// &funcres,
|
|
|
|
// )
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
2016-01-17 15:29:30 +00:00
|
|
|
|
|
|
|
fact := factoid{
|
|
|
|
fact: strings.ToLower(trigger),
|
|
|
|
verb: "reply",
|
|
|
|
tidbit: msg,
|
|
|
|
owner: user.Name,
|
|
|
|
created: time.Now(),
|
|
|
|
accessed: time.Now(),
|
|
|
|
count: 0,
|
2012-08-20 01:28:25 +00:00
|
|
|
}
|
2016-01-15 06:12:26 +00:00
|
|
|
if err := p.Coll.Insert(fact); err != nil {
|
2013-09-01 03:05:00 +00:00
|
|
|
log.Println("ERROR!!!!:", err)
|
|
|
|
}
|
|
|
|
|
2013-09-01 03:33:15 +00:00
|
|
|
log.Println("Remembering factoid:", msg)
|
|
|
|
|
2013-09-01 03:05:00 +00:00
|
|
|
// sorry, not creative with names so we're reusing msg
|
|
|
|
msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
|
|
|
|
message.User.Name, msg)
|
|
|
|
p.Bot.SendMessage(message.Channel, msg)
|
|
|
|
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
|
|
|
return true
|
2012-08-20 01:28:25 +00:00
|
|
|
}
|
2013-09-01 03:05:00 +00:00
|
|
|
|
2012-08-20 01:28:25 +00:00
|
|
|
p.Bot.SendMessage(message.Channel, "Sorry, I don't know that phrase.")
|
2013-06-17 03:44:24 +00:00
|
|
|
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
|
|
|
return true
|
2012-08-20 01:28:25 +00:00
|
|
|
}
|
2012-08-23 20:28:45 +00:00
|
|
|
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
2012-08-20 01:28:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-05-07 23:28:34 +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.
|
2012-08-20 01:28:25 +00:00
|
|
|
func (p *RememberPlugin) LoadData() {
|
2016-01-15 06:12:26 +00:00
|
|
|
// Mongo is removed, this plugin will crash if started
|
|
|
|
log.Fatal("The Remember plugin has not been upgraded to SQL yet.")
|
|
|
|
// p.Coll = p.Bot.Db.C("factoid")
|
2012-08-20 01:28:25 +00:00
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *RememberPlugin) Help(channel string, parts []string) {
|
2013-05-07 23:28:34 +00:00
|
|
|
|
|
|
|
msg := "!remember will let you quote your idiot friends. Just type " +
|
2013-05-07 23:31:16 +00:00
|
|
|
"!remember <nick> <snippet> to remember what they said. Snippet can " +
|
|
|
|
"be any part of their message. Later on, you can ask for a random " +
|
|
|
|
"!quote."
|
2013-05-07 23:28:34 +00:00
|
|
|
|
2012-08-20 01:28:25 +00:00
|
|
|
p.Bot.SendMessage(channel, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deliver a random quote out of the db.
|
2013-05-07 23:28:34 +00:00
|
|
|
// Note: this is the same cache for all channels joined. This plugin needs to be
|
|
|
|
// expanded to have this function execute a quote for a particular channel
|
2012-08-20 01:28:25 +00:00
|
|
|
func (p *RememberPlugin) randQuote() string {
|
2016-01-17 15:29:30 +00:00
|
|
|
var quotes []factoid
|
2012-08-27 15:34:21 +00:00
|
|
|
// todo: find anything with the word "quotes" in the trigger
|
|
|
|
query := bson.M{
|
|
|
|
"trigger": bson.M{
|
|
|
|
"$regex": "quotes$",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
iter := p.Coll.Find(query).Iter()
|
2012-08-20 01:28:25 +00:00
|
|
|
err := iter.All("es)
|
|
|
|
if err != nil {
|
|
|
|
panic(iter.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
// rand quote idx
|
|
|
|
nquotes := len(quotes)
|
2012-08-23 20:28:45 +00:00
|
|
|
if nquotes == 0 {
|
|
|
|
return "Sorry, I don't know any quotes."
|
|
|
|
}
|
2012-08-20 01:28:25 +00:00
|
|
|
quote := quotes[rand.Intn(nquotes)]
|
2016-01-17 15:29:30 +00:00
|
|
|
return quote.tidbit
|
2012-08-20 01:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RememberPlugin) quoteTimer(channel string) {
|
|
|
|
for {
|
2012-08-23 20:28:45 +00:00
|
|
|
// this pisses me off: You can't multiply int * time.Duration so it
|
|
|
|
// has to look ugly as shit.
|
|
|
|
time.Sleep(time.Duration(p.Bot.Config.QuoteTime) * time.Minute)
|
2012-08-20 01:28:25 +00:00
|
|
|
chance := 1.0 / p.Bot.Config.QuoteChance
|
|
|
|
if rand.Intn(int(chance)) == 0 {
|
|
|
|
msg := p.randQuote()
|
|
|
|
p.Bot.SendMessage(channel, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-25 04:46:13 +00:00
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
2012-08-25 04:49:48 +00:00
|
|
|
func (p *RememberPlugin) Event(kind string, message bot.Message) bool {
|
2012-08-25 04:46:13 +00:00
|
|
|
return false
|
|
|
|
}
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Record what the bot says in the log
|
|
|
|
func (p *RememberPlugin) BotMessage(message bot.Message) bool {
|
|
|
|
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
|
|
|
return false
|
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *RememberPlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|