irc: Update IRC connector to new structure

This commit is contained in:
Chris Sexton 2019-02-05 11:20:43 -05:00
parent e7c88c0c9c
commit 3620208f33
3 changed files with 26 additions and 28 deletions

View File

@ -34,12 +34,16 @@ type kind int
type Callback func(int, msg.Message, ...interface{}) bool type Callback func(int, msg.Message, ...interface{}) bool
// Bot interface serves to allow mocking of the actual bot
type Bot interface { type Bot interface {
// Config allows access to the bot's configuration system
Config() *config.Config Config() *config.Config
// DB gives access to the current database
DB() *sqlx.DB DB() *sqlx.DB
// Who lists users in a particular channel
Who(string) []user.User Who(string) []user.User
// AddPlugin registers a new plugin handler
AddPlugin(string, Plugin) AddPlugin(string, Plugin)
// First arg should be one of bot.Message/Reply/Action/etc // First arg should be one of bot.Message/Reply/Action/etc
Send(int, ...interface{}) (error, string) Send(int, ...interface{}) (error, string)
// First arg should be one of bot.Message/Reply/Action/etc // First arg should be one of bot.Message/Reply/Action/etc
@ -55,6 +59,7 @@ type Bot interface {
RegisterFilter(string, func(string) string) RegisterFilter(string, func(string) string)
} }
// Connector represents a server connection to a chat service
type Connector interface { type Connector interface {
RegisterEventReceived(func(message msg.Message)) RegisterEventReceived(func(message msg.Message))
RegisterMessageReceived(func(message msg.Message)) RegisterMessageReceived(func(message msg.Message))
@ -68,7 +73,8 @@ type Connector interface {
Who(string) []string Who(string) []string
} }
// Interface used for compatibility with the Plugin interface // Plugin interface used for compatibility with the Plugin interface
// Probably can disappear once RegisterWeb gets inverted
type Plugin interface { type Plugin interface {
RegisterWeb() *string RegisterWeb() *string
} }

View File

@ -66,12 +66,24 @@ func (i *Irc) RegisterReplyMessageReceived(f func(msg.Message, string)) {
i.replyMessageReceived = f i.replyMessageReceived = f
} }
func (i *Irc) Send(kind int, args ...interface{}) (error, string) {
switch kind {
case bot.Reply:
case bot.Message:
return i.sendMessage(args[0].(string), args[1].(string))
case bot.Action:
return i.sendAction(args[0].(string), args[1].(string))
default:
}
return nil, ""
}
func (i *Irc) JoinChannel(channel string) { func (i *Irc) JoinChannel(channel string) {
log.Printf("Joining channel: %s", channel) log.Printf("Joining channel: %s", channel)
i.Client.Out <- irc.Msg{Cmd: irc.JOIN, Args: []string{channel}} i.Client.Out <- irc.Msg{Cmd: irc.JOIN, Args: []string{channel}}
} }
func (i *Irc) SendMessage(channel, message string) string { func (i *Irc) sendMessage(channel, message string) (error, string) {
for len(message) > 0 { for len(message) > 0 {
m := irc.Msg{ m := irc.Msg{
Cmd: "PRIVMSG", Cmd: "PRIVMSG",
@ -95,37 +107,18 @@ func (i *Irc) SendMessage(channel, message string) string {
i.Client.Out <- m i.Client.Out <- m
} }
return "NO_IRC_IDENTIFIERS" return nil, "NO_IRC_IDENTIFIERS"
} }
// Sends action to channel // Sends action to channel
func (i *Irc) SendAction(channel, message string) string { func (i *Irc) sendAction(channel, message string) (error, string) {
message = actionPrefix + " " + message + "\x01" message = actionPrefix + " " + message + "\x01"
i.SendMessage(channel, message) return i.sendMessage(channel, message)
return "NO_IRC_IDENTIFIERS"
}
func (i *Irc) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) {
return "NO_IRC_IDENTIFIERS", false
}
func (i *Irc) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) {
return "NO_IRC_IDENTIFIERS", false
}
func (i *Irc) React(channel, reaction string, message msg.Message) bool {
//we're not goign to do anything because it's IRC
return false
}
func (i *Irc) Edit(channel, newMessage, identifier string) bool {
//we're not goign to do anything because it's IRC
return false
} }
func (i *Irc) GetEmojiList() map[string]string { func (i *Irc) GetEmojiList() map[string]string {
//we're not goign to do anything because it's IRC //we're not going to do anything because it's IRC
return make(map[string]string) return make(map[string]string)
} }

View File

@ -35,7 +35,6 @@ import (
"github.com/velour/catbase/plugins/twitch" "github.com/velour/catbase/plugins/twitch"
"github.com/velour/catbase/plugins/your" "github.com/velour/catbase/plugins/your"
"github.com/velour/catbase/plugins/zork" "github.com/velour/catbase/plugins/zork"
"github.com/velour/catbase/slack"
) )
var ( var (
@ -71,7 +70,7 @@ func main() {
case "irc": case "irc":
client = irc.New(c) client = irc.New(c)
case "slack": case "slack":
client = slack.New(c) //client = slack.New(c)
default: default:
log.Fatalf("Unknown connection type: %s", c.Get("type", "UNSET")) log.Fatalf("Unknown connection type: %s", c.Get("type", "UNSET"))
} }