Added generic Event handling to plugins, made Talker handle the OCD hivemind

This commit is contained in:
Chris Sexton 2012-08-25 00:46:13 -04:00
parent e4c755faaa
commit 9c721a0f58
7 changed files with 71 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import irc "github.com/fluffle/goirc/client"
// Interface used for compatibility with the Plugin interface // Interface used for compatibility with the Plugin interface
type Handler interface { type Handler interface {
Message(message Message) bool Message(message Message) bool
Event(kind string, message Message) bool
Help(channel string, parts []string) Help(channel string, parts []string)
} }
@ -85,10 +86,9 @@ func (b *Bot) isCmd(message string) (bool, string) {
return iscmd, message return iscmd, message
} }
// Handles incomming PRIVMSG requests // Builds our internal message type out of a Conn & Line from irc
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { func (b *Bot)buildMessage(conn *irc.Conn, line *irc.Line) Message {
// Check for the user // Check for the user
user := b.checkuser(line.Nick) user := b.checkuser(line.Nick)
channel := line.Args[0] channel := line.Args[0]
@ -98,21 +98,18 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
isaction := line.Cmd == "ACTION" isaction := line.Cmd == "ACTION"
message := line.Args[1] var message string
if len(line.Args) > 1 {
message = line.Args[1]
}
iscmd := false iscmd := false
filteredMessage := message filteredMessage := message
if !isaction { if !isaction {
iscmd, filteredMessage = b.isCmd(message) iscmd, filteredMessage = b.isCmd(message)
} }
parts := strings.Fields(strings.ToLower(filteredMessage))
user.MessageLog = append(user.MessageLog, message) user.MessageLog = append(user.MessageLog, message)
if strings.HasPrefix(filteredMessage, "help") && iscmd{
b.checkHelp(channel, parts)
return
}
msg := Message{ msg := Message{
User: user, User: user,
Channel: channel, Channel: channel,
@ -121,6 +118,19 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
Command: iscmd, Command: iscmd,
Action: isaction, Action: isaction,
} }
return msg
}
// Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
msg := b.buildMessage(conn, line)
if strings.HasPrefix(msg.Body, "help") && msg.Command{
parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts)
return
}
for _, p := range b.Plugins { for _, p := range b.Plugins {
if p.Message(msg) { if p.Message(msg) {
break break
@ -151,3 +161,12 @@ func (b *Bot) Help(channel string, parts []string) {
"http://bitbucket.org/phlyingpenguin/godeepintir", b.Version) "http://bitbucket.org/phlyingpenguin/godeepintir", b.Version)
b.SendMessage(channel, msg) b.SendMessage(channel, msg)
} }
func (b *Bot) UserJoined(conn *irc.Conn, line *irc.Line) {
msg := b.buildMessage(conn, line)
for _, p := range b.Plugins {
if p.Event(line.Cmd, msg) {
break
}
}
}

View File

@ -52,6 +52,10 @@ func main() {
b.MsgRecieved(conn, line) b.MsgRecieved(conn, line)
}) })
c.AddHandler("JOIN", func(conn *irc.Conn, line *irc.Line) {
b.UserJoined(conn, line)
})
c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
b.MsgRecieved(conn, line) b.MsgRecieved(conn, line)
}) })

View File

@ -129,6 +129,11 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
return false return false
} }
// Empty event handler because this plugin does not do anything on event recv
func (p *BeersPlugin) Event(kind string, message bot.Message) bool {
return false
}
// LoadData imports any configuration data into the plugin. This is not strictly necessary other // 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 // than the fact that the Plugin interface demands it exist. This may be deprecated at a later
// date. // date.

View File

@ -6,6 +6,7 @@ import "bitbucket.org/phlyingpenguin/godeepintir/bot"
// Plugin interface defines the methods needed to accept a plugin // Plugin interface defines the methods needed to accept a plugin
type Plugin interface { type Plugin interface {
Message(message bot.Message) bool Message(message bot.Message) bool
Event(kind string, message bot.Message) bool
LoadData() LoadData()
Help() Help()
} }
@ -55,6 +56,11 @@ func (p *TestPlugin) Help(message bot.Message) {
} }
} }
// Empty event handler because this plugin does not do anything on event recv
func (p *TestPlugin) Event(kind string, message bot.Message) bool {
return false
}
type PluginConfig struct { type PluginConfig struct {
Name string Name string
Values map[string]interface{} Values map[string]interface{}
@ -83,3 +89,9 @@ func (fp FalsePlugin) Message(user, message string) bool {
func (fp FalsePlugin) LoadData() { func (fp FalsePlugin) LoadData() {
} }
// Empty event handler because this plugin does not do anything on event recv
func (p *FalsePlugin) Event(kind string, message bot.Message) bool {
return false
}

View File

@ -157,3 +157,8 @@ func (p *RememberPlugin) quoteTimer(channel string) {
} }
} }
} }
// Empty event handler because this plugin does not do anything on event recv
func (p *RememberPlugin) Event(kind string, message bot.Message) bool {
return false
}

View File

@ -34,3 +34,8 @@ func (p *SkeletonPlugin) LoadData() {
func (p *SkeletonPlugin) Help(channel string, parts []string) { func (p *SkeletonPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Sorry, Skeleton does not do a goddamn thing.") p.Bot.SendMessage(channel, "Sorry, Skeleton does not do a goddamn thing.")
} }
// Empty event handler because this plugin does not do anything on event recv
func (p *SkeletonPlugin) Event(kind string, message bot.Message) bool {
return false
}

View File

@ -3,6 +3,7 @@ package plugins
import ( import (
"bitbucket.org/phlyingpenguin/godeepintir/bot" "bitbucket.org/phlyingpenguin/godeepintir/bot"
"strings" "strings"
"fmt"
) )
type TalkerPlugin struct { type TalkerPlugin struct {
@ -41,3 +42,13 @@ func (p *TalkerPlugin) LoadData() {
func (p *TalkerPlugin) Help(channel string, parts []string) { func (p *TalkerPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!") p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!")
} }
// 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 {
msg := fmt.Sprintf("Joins upset the hivemind's OCD, %s.", message.User.Name)
p.Bot.SendMessage(message.Channel, msg)
return true
}
return false
}