From 66e2c8265d79d6af4cc02018345a440eba865db1 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 22 Jan 2013 16:14:04 -0500 Subject: [PATCH] Added functionality for downtime tracking --- bot/bot.go | 2 +- main.go | 1 + plugins/downtime.go | 62 ++++++++++++++++++++++++++++++++++++++++++--- plugins/talker.go | 2 +- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 3c32073..9a6bef6 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -114,7 +114,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { RunNewLogger(logIn, logOut) - config.Nick = c.Me.Name + config.Nick = c.Me.Nick return &Bot{ Config: config, diff --git a/main.go b/main.go index f21b85b..ba1a2e1 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ func main() { b := bot.NewBot(config, c) // b.AddHandler(plugins.NewTestPlugin(b)) b.AddHandler("admin", plugins.NewAdminPlugin(b)) + b.AddHandler("downtime", plugins.NewDowntimePlugin(b)) b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("dice", plugins.NewDicePlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b)) diff --git a/plugins/downtime.go b/plugins/downtime.go index 94fc045..e4517a4 100644 --- a/plugins/downtime.go +++ b/plugins/downtime.go @@ -3,7 +3,11 @@ package plugins import "bitbucket.org/phlyingpenguin/godeepintir/bot" import ( + "fmt" "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "strings" + "time" ) // This is a downtime plugin to monitor how much our users suck @@ -13,19 +17,65 @@ type DowntimePlugin struct { Coll *mgo.Collection } +type idleEntry struct { + Nick string + LastSeen time.Time +} + // NewDowntimePlugin creates a new DowntimePlugin with the Plugin interface func NewDowntimePlugin(bot *bot.Bot) *DowntimePlugin { - return &DowntimePlugin{ + p := DowntimePlugin{ Bot: bot, } + p.LoadData() + return &p } // 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 { - // This bot does not reply to anything - return false + // If it's a command and the payload is idle , 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 + } + + 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 + p.Coll.Insert(idleEntry{ + Nick: user, + LastSeen: time.Now(), + }) + } else { + // Update their entry, they were baaaaaad + entry.LastSeen = time.Now() + p.Coll.Upsert(bson.M{"nick": entry.Nick}, entry) + + } } // LoadData imports any configuration data into the plugin. This is not strictly necessary other @@ -37,10 +87,14 @@ func (p *DowntimePlugin) LoadData() { // Help responds to help requests. Every plugin must implement a help function. func (p *DowntimePlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Sorry, Downtime does not do a goddamn thing.") + p.Bot.SendMessage(channel, "Ask me how long one of your friends has been idele with, \"idle \"") } // Empty event handler because this plugin does not do anything on event recv func (p *DowntimePlugin) Event(kind string, message bot.Message) bool { + if kind == "JOIN" && message.User.Name != p.Bot.Config.Nick { + // user joined, let's nail them for it + p.record(strings.ToLower(message.User.Name)) + } return false } diff --git a/plugins/talker.go b/plugins/talker.go index d3ed9cb..71cb92c 100644 --- a/plugins/talker.go +++ b/plugins/talker.go @@ -54,7 +54,7 @@ func (p *TalkerPlugin) Help(channel string, parts []string) { // Empty event handler because this plugin does not do anything on event recv func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { - if kind == "JOIN" && message.User.Name != p.Bot.Config.Nick { + if kind == "JOIN" && strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config.Nick) { sayings := p.Bot.Config.WelcomeMsgs msg := fmt.Sprintf(sayings[rand.Intn(len(sayings))], message.User.Name) p.Bot.SendMessage(message.Channel, msg)