2012-08-31 22:55:05 +00:00
|
|
|
package plugins
|
|
|
|
|
2013-01-22 19:46:51 +00:00
|
|
|
import "bitbucket.org/phlyingpenguin/godeepintir/bot"
|
|
|
|
|
|
|
|
import (
|
2013-01-22 21:14:04 +00:00
|
|
|
"fmt"
|
2013-01-22 19:46:51 +00:00
|
|
|
"labix.org/v2/mgo"
|
2013-01-22 21:14:04 +00:00
|
|
|
"labix.org/v2/mgo/bson"
|
2013-01-23 00:22:32 +00:00
|
|
|
"log"
|
2013-01-28 18:35:41 +00:00
|
|
|
"sort"
|
2013-01-22 21:14:04 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2013-01-22 19:46:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a downtime plugin to monitor how much our users suck
|
|
|
|
|
|
|
|
type DowntimePlugin struct {
|
|
|
|
Bot *bot.Bot
|
|
|
|
Coll *mgo.Collection
|
|
|
|
}
|
|
|
|
|
2013-01-22 21:14:04 +00:00
|
|
|
type idleEntry struct {
|
|
|
|
Nick string
|
|
|
|
LastSeen time.Time
|
|
|
|
}
|
|
|
|
|
2013-01-23 15:53:31 +00:00
|
|
|
type idleEntries []*idleEntry
|
|
|
|
|
|
|
|
func (ie idleEntries) Len() int {
|
|
|
|
return len(ie)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ie idleEntries) Less(i, j int) bool {
|
2013-01-28 18:39:21 +00:00
|
|
|
return ie[i].LastSeen.Before(ie[j].LastSeen)
|
2013-01-23 15:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ie idleEntries) Swap(i, j int) {
|
|
|
|
ie[i], ie[j] = ie[j], ie[i]
|
|
|
|
}
|
|
|
|
|
2013-01-22 19:46:51 +00:00
|
|
|
// NewDowntimePlugin creates a new DowntimePlugin with the Plugin interface
|
|
|
|
func NewDowntimePlugin(bot *bot.Bot) *DowntimePlugin {
|
2013-01-22 21:14:04 +00:00
|
|
|
p := DowntimePlugin{
|
2013-01-22 19:46:51 +00:00
|
|
|
Bot: bot,
|
|
|
|
}
|
2013-01-22 21:14:04 +00:00
|
|
|
p.LoadData()
|
|
|
|
return &p
|
2013-01-22 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 *DowntimePlugin) Message(message bot.Message) bool {
|
2013-01-22 21:14:04 +00:00
|
|
|
// If it's a command and the payload is idle <nick>, give it. Log everything.
|
|
|
|
|
|
|
|
parts := strings.Fields(strings.ToLower(message.Body))
|
|
|
|
channel := message.Channel
|
|
|
|
ret := false
|
|
|
|
|
|
|
|
if parts[0] == "idle" && len(parts) == 2 {
|
|
|
|
nick := parts[1]
|
|
|
|
// parts[1] must be the userid, or we don't know them
|
|
|
|
var entry idleEntry
|
|
|
|
p.Coll.Find(bson.M{"nick": nick}).One(&entry)
|
|
|
|
if entry.Nick != nick {
|
|
|
|
// couldn't find em
|
|
|
|
p.Bot.SendMessage(channel, fmt.Sprintf("Sorry, I don't know %s.", nick))
|
|
|
|
} else {
|
|
|
|
p.Bot.SendMessage(channel, fmt.Sprintf("%s has been idle for: %s",
|
|
|
|
nick, time.Now().Sub(entry.LastSeen)))
|
|
|
|
}
|
|
|
|
ret = true
|
2013-01-23 15:53:31 +00:00
|
|
|
} else if parts[0] == "idle" && len(parts) == 1 {
|
|
|
|
// Find all idle times, report them.
|
2013-01-28 18:35:41 +00:00
|
|
|
var entries idleEntries
|
|
|
|
p.Coll.Find(nil).All(&entries)
|
|
|
|
sort.Sort(entries)
|
|
|
|
tops := "The top entries are: "
|
|
|
|
for _, e := range entries {
|
|
|
|
// filter out ZNC entries
|
|
|
|
if !strings.HasPrefix(e.Nick, "*") {
|
|
|
|
tops = fmt.Sprintf("%s%s: %s ", tops, e.Nick, time.Now().Sub(e.LastSeen))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Bot.SendMessage(channel, tops)
|
2013-01-28 18:48:13 +00:00
|
|
|
ret = true
|
2013-01-28 18:35:41 +00:00
|
|
|
|
2013-01-22 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.record(strings.ToLower(message.User.Name))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DowntimePlugin) record(user string) {
|
|
|
|
var entry idleEntry
|
|
|
|
p.Coll.Find(bson.M{"nick": user}).One(&entry)
|
|
|
|
if entry.Nick != user {
|
|
|
|
// insert a new entry
|
2013-01-28 18:39:21 +00:00
|
|
|
p.Coll.Upsert(bson.M{"nick": user}, idleEntry{
|
2013-01-22 21:14:04 +00:00
|
|
|
Nick: user,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
})
|
2013-01-23 00:22:32 +00:00
|
|
|
log.Println("Inserted downtime for:", user)
|
2013-01-22 21:14:04 +00:00
|
|
|
} else {
|
|
|
|
// Update their entry, they were baaaaaad
|
|
|
|
entry.LastSeen = time.Now()
|
|
|
|
p.Coll.Upsert(bson.M{"nick": entry.Nick}, entry)
|
|
|
|
}
|
2013-01-22 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:22:32 +00:00
|
|
|
func (p *DowntimePlugin) remove(user string) {
|
|
|
|
p.Coll.RemoveAll(bson.M{"nick": user})
|
|
|
|
log.Println("Removed downtime for:", user)
|
|
|
|
}
|
|
|
|
|
2013-01-22 19:46:51 +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 *DowntimePlugin) LoadData() {
|
|
|
|
p.Coll = p.Bot.Db.C("downtime")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *DowntimePlugin) Help(channel string, parts []string) {
|
2013-01-22 21:14:04 +00:00
|
|
|
p.Bot.SendMessage(channel, "Ask me how long one of your friends has been idele with, \"idle <nick>\"")
|
2013-01-22 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
|
|
|
func (p *DowntimePlugin) Event(kind string, message bot.Message) bool {
|
2013-01-23 00:22:32 +00:00
|
|
|
log.Println(kind, "\t", message)
|
|
|
|
if kind != "PART" && message.User.Name != p.Bot.Config.Nick {
|
2013-01-22 21:14:04 +00:00
|
|
|
// user joined, let's nail them for it
|
2013-01-23 00:22:32 +00:00
|
|
|
if kind == "NICK" {
|
|
|
|
p.record(strings.ToLower(message.Channel))
|
|
|
|
p.remove(strings.ToLower(message.User.Name))
|
|
|
|
} else {
|
|
|
|
p.record(strings.ToLower(message.User.Name))
|
|
|
|
}
|
|
|
|
} else if kind == "PART" {
|
|
|
|
p.remove(strings.ToLower(message.User.Name))
|
2013-01-23 15:53:31 +00:00
|
|
|
} else {
|
|
|
|
log.Println("Unknown event: ", message)
|
|
|
|
p.record(strings.ToLower(message.User.Name))
|
2013-01-22 21:14:04 +00:00
|
|
|
}
|
2013-01-22 19:46:51 +00:00
|
|
|
return false
|
|
|
|
}
|