diff --git a/bot/handlers.go b/bot/handlers.go index 67ee387..3c8d329 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -9,6 +9,7 @@ import irc "github.com/fluffle/goirc/client" // Interface used for compatibility with the Plugin interface type Handler interface { Message(message Message) bool + Event(kind string, message Message) bool Help(channel string, parts []string) } @@ -85,10 +86,9 @@ func (b *Bot) isCmd(message string) (bool, string) { return iscmd, message } -// Handles incomming PRIVMSG requests -func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { +// Builds our internal message type out of a Conn & Line from irc +func (b *Bot)buildMessage(conn *irc.Conn, line *irc.Line) Message { // Check for the user - user := b.checkuser(line.Nick) channel := line.Args[0] @@ -98,21 +98,18 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { isaction := line.Cmd == "ACTION" - message := line.Args[1] + var message string + if len(line.Args) > 1 { + message = line.Args[1] + } iscmd := false filteredMessage := message if !isaction { iscmd, filteredMessage = b.isCmd(message) } - parts := strings.Fields(strings.ToLower(filteredMessage)) user.MessageLog = append(user.MessageLog, message) - if strings.HasPrefix(filteredMessage, "help") && iscmd{ - b.checkHelp(channel, parts) - return - } - msg := Message{ User: user, Channel: channel, @@ -121,6 +118,19 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { Command: iscmd, 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 { if p.Message(msg) { break @@ -151,3 +161,12 @@ func (b *Bot) Help(channel string, parts []string) { "http://bitbucket.org/phlyingpenguin/godeepintir", b.Version) 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 + } + } +} \ No newline at end of file diff --git a/main.go b/main.go index a0535a8..20f4481 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,10 @@ func main() { 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) { b.MsgRecieved(conn, line) }) diff --git a/plugins/beers.go b/plugins/beers.go index cc98eca..abf08ff 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -129,6 +129,11 @@ func (p *BeersPlugin) Message(message bot.Message) bool { 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 // than the fact that the Plugin interface demands it exist. This may be deprecated at a later // date. diff --git a/plugins/plugins.go b/plugins/plugins.go index 28bec68..0be6647 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -6,6 +6,7 @@ import "bitbucket.org/phlyingpenguin/godeepintir/bot" // Plugin interface defines the methods needed to accept a plugin type Plugin interface { Message(message bot.Message) bool + Event(kind string, message bot.Message) bool LoadData() 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 { Name string Values map[string]interface{} @@ -83,3 +89,9 @@ func (fp FalsePlugin) Message(user, message string) bool { 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 +} + diff --git a/plugins/remember.go b/plugins/remember.go index 76aed55..62861ff 100644 --- a/plugins/remember.go +++ b/plugins/remember.go @@ -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 +} diff --git a/plugins/skeleton.go b/plugins/skeleton.go index bac69e1..c61a41a 100644 --- a/plugins/skeleton.go +++ b/plugins/skeleton.go @@ -34,3 +34,8 @@ func (p *SkeletonPlugin) LoadData() { func (p *SkeletonPlugin) Help(channel string, parts []string) { 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 +} diff --git a/plugins/talker.go b/plugins/talker.go index 23de2e0..902315b 100644 --- a/plugins/talker.go +++ b/plugins/talker.go @@ -3,6 +3,7 @@ package plugins import ( "bitbucket.org/phlyingpenguin/godeepintir/bot" "strings" + "fmt" ) type TalkerPlugin struct { @@ -41,3 +42,13 @@ func (p *TalkerPlugin) LoadData() { func (p *TalkerPlugin) Help(channel string, parts []string) { 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 +}