diff options
Diffstat (limited to 'bridge/slack')
-rw-r--r-- | bridge/slack/slack.go | 79 |
1 files changed, 49 insertions, 30 deletions
diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 804eaea4..fbaff703 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -15,41 +15,46 @@ type MMMessage struct { Username string } -type bslack struct { +type Bslack struct { mh *matterhook.Client sc *slack.Client // MMapi - *config.Config + Config *config.Protocol rtm *slack.RTM Plus bool Remote chan config.Message + protocol string + origin string channels []slack.Channel } var flog *log.Entry +var protocol = "slack" func init() { - flog = log.WithFields(log.Fields{"module": "slack"}) + flog = log.WithFields(log.Fields{"module": protocol}) } -func New(cfg *config.Config, c chan config.Message) *bslack { - b := &bslack{} - b.Config = cfg +func New(config config.Protocol, origin string, c chan config.Message) *Bslack { + b := &Bslack{} + b.Config = &config b.Remote = c - b.Plus = cfg.Slack.UseAPI + b.protocol = protocol + b.origin = origin + b.Plus = config.UseAPI return b } -func (b *bslack) Command(cmd string) string { +func (b *Bslack) Command(cmd string) string { return "" } -func (b *bslack) Connect() error { +func (b *Bslack) Connect() error { if !b.Plus { - b.mh = matterhook.New(b.Config.Slack.URL, - matterhook.Config{BindAddress: b.Config.Slack.BindAddress}) + b.mh = matterhook.New(b.Config.URL, + matterhook.Config{BindAddress: b.Config.BindAddress}) } else { - b.sc = slack.New(b.Config.Slack.Token) + b.sc = slack.New(b.Config.Token) flog.Infof("Trying login on slack with Token") /* if err != nil { @@ -64,11 +69,32 @@ func (b *bslack) Connect() error { return nil } -func (b *bslack) Name() string { - return "slack" +func (b *Bslack) FullOrigin() string { + return b.protocol + "." + b.origin } -func (b *bslack) Send(msg config.Message) error { +func (b *Bslack) JoinChannel(channel string) error { + schannel := b.getChannelByName(channel) + if schannel != nil && !schannel.IsMember { + flog.Infof("Joining %s", channel) + b.sc.JoinChannel(schannel.ID) + } + return nil +} + +func (b *Bslack) Name() string { + return b.protocol + "." + b.origin +} + +func (b *Bslack) Protocol() string { + return b.protocol +} + +func (b *Bslack) Origin() string { + return b.origin +} + +func (b *Bslack) Send(msg config.Message) error { flog.Infof("slack send %#v", msg) if msg.Origin != "slack" { return b.SendType(msg.Username, msg.Text, msg.Channel, "") @@ -76,12 +102,12 @@ func (b *bslack) Send(msg config.Message) error { return nil } -func (b *bslack) SendType(nick string, message string, channel string, mtype string) error { - if b.Config.Slack.PrefixMessagesWithNick { +func (b *Bslack) SendType(nick string, message string, channel string, mtype string) error { + if b.Config.PrefixMessagesWithNick { message = nick + " " + message } if !b.Plus { - matterMessage := matterhook.OMessage{IconURL: b.Config.Slack.IconURL} + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage.Channel = channel matterMessage.UserName = nick matterMessage.Type = mtype @@ -100,7 +126,7 @@ func (b *bslack) SendType(nick string, message string, channel string, mtype str return nil } -func (b *bslack) getChannelByName(name string) *slack.Channel { +func (b *Bslack) getChannelByName(name string) *slack.Channel { if b.channels == nil { return nil } @@ -112,7 +138,7 @@ func (b *bslack) getChannelByName(name string) *slack.Channel { return nil } -func (b *bslack) handleSlack() { +func (b *Bslack) handleSlack() { flog.Infof("Choosing API based slack connection: %t", b.Plus) mchan := make(chan *MMMessage) if b.Plus { @@ -126,12 +152,12 @@ func (b *bslack) handleSlack() { texts := strings.Split(message.Text, "\n") for _, text := range texts { flog.Debug("Sending message from " + message.Username + " to " + message.Channel) - b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: "slack"} + b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} } } } -func (b *bslack) handleSlackClient(mchan chan *MMMessage) { +func (b *Bslack) handleSlackClient(mchan chan *MMMessage) { for msg := range b.rtm.IncomingEvents { switch ev := msg.Data.(type) { case *slack.MessageEvent: @@ -153,13 +179,6 @@ func (b *bslack) handleSlackClient(mchan chan *MMMessage) { flog.Debugf("%#v", ev.Error()) case *slack.ConnectedEvent: b.channels = ev.Info.Channels - for _, val := range b.Config.Channel { - channel := b.getChannelByName(val.Slack) - if channel != nil && !channel.IsMember { - flog.Infof("Joining %s", val.Slack) - b.sc.JoinChannel(channel.ID) - } - } case *slack.InvalidAuthEvent: flog.Fatalf("Invalid Token %#v", ev) default: @@ -167,7 +186,7 @@ func (b *bslack) handleSlackClient(mchan chan *MMMessage) { } } -func (b *bslack) handleMatterHook(mchan chan *MMMessage) { +func (b *Bslack) handleMatterHook(mchan chan *MMMessage) { for { message := b.mh.Receive() flog.Debugf("receiving from slack %#v", message) |