2016-09-05 16:34:37 +02:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/nlopes/slack"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MMMessage struct {
|
|
|
|
Text string
|
|
|
|
Channel string
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
type Bslack struct {
|
2016-09-05 16:34:37 +02:00
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
|
|
|
// MMapi
|
2016-09-18 19:21:15 +02:00
|
|
|
Config *config.Protocol
|
2016-09-05 16:34:37 +02:00
|
|
|
rtm *slack.RTM
|
|
|
|
Plus bool
|
|
|
|
Remote chan config.Message
|
2016-09-18 19:21:15 +02:00
|
|
|
protocol string
|
|
|
|
origin string
|
2016-09-05 16:34:37 +02:00
|
|
|
channels []slack.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
2016-09-18 19:21:15 +02:00
|
|
|
var protocol = "slack"
|
2016-09-05 16:34:37 +02:00
|
|
|
|
|
|
|
func init() {
|
2016-09-18 19:21:15 +02:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func New(config config.Protocol, origin string, c chan config.Message) *Bslack {
|
|
|
|
b := &Bslack{}
|
|
|
|
b.Config = &config
|
2016-09-05 16:34:37 +02:00
|
|
|
b.Remote = c
|
2016-09-18 19:21:15 +02:00
|
|
|
b.protocol = protocol
|
|
|
|
b.origin = origin
|
|
|
|
b.Plus = config.UseAPI
|
2016-09-05 16:34:37 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) Command(cmd string) string {
|
2016-09-05 16:34:37 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) Connect() error {
|
2016-09-05 16:34:37 +02:00
|
|
|
if !b.Plus {
|
2016-09-18 19:21:15 +02:00
|
|
|
b.mh = matterhook.New(b.Config.URL,
|
|
|
|
matterhook.Config{BindAddress: b.Config.BindAddress})
|
2016-09-05 16:34:37 +02:00
|
|
|
} else {
|
2016-09-18 19:21:15 +02:00
|
|
|
b.sc = slack.New(b.Config.Token)
|
2016-09-05 16:34:37 +02:00
|
|
|
flog.Infof("Trying login on slack with Token")
|
|
|
|
/*
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
flog.Info("Login ok")
|
|
|
|
}
|
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
|
|
|
go b.handleSlack()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) FullOrigin() string {
|
|
|
|
return b.protocol + "." + b.origin
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
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 {
|
2016-09-05 16:34:37 +02:00
|
|
|
flog.Infof("slack send %#v", msg)
|
|
|
|
if msg.Origin != "slack" {
|
|
|
|
return b.SendType(msg.Username, msg.Text, msg.Channel, "")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) SendType(nick string, message string, channel string, mtype string) error {
|
|
|
|
if b.Config.PrefixMessagesWithNick {
|
2016-09-05 16:34:37 +02:00
|
|
|
message = nick + " " + message
|
|
|
|
}
|
|
|
|
if !b.Plus {
|
2016-09-18 19:21:15 +02:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
2016-09-05 16:34:37 +02:00
|
|
|
matterMessage.Channel = channel
|
|
|
|
matterMessage.UserName = nick
|
|
|
|
matterMessage.Type = mtype
|
|
|
|
matterMessage.Text = message
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
|
|
|
flog.Info(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
flog.Debug("->slack channel: ", channel, " ", message)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
flog.Debugf("sent to slack channel API: %s %s", channel, message)
|
|
|
|
newmsg := b.rtm.NewOutgoingMessage(message, b.getChannelByName(channel).ID)
|
|
|
|
b.rtm.SendMessage(newmsg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) getChannelByName(name string) *slack.Channel {
|
2016-09-05 16:34:37 +02:00
|
|
|
if b.channels == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.Name == name {
|
|
|
|
return &channel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleSlack() {
|
2016-09-05 16:34:37 +02:00
|
|
|
flog.Infof("Choosing API based slack connection: %t", b.Plus)
|
|
|
|
mchan := make(chan *MMMessage)
|
|
|
|
if b.Plus {
|
|
|
|
go b.handleSlackClient(mchan)
|
|
|
|
} else {
|
|
|
|
go b.handleMatterHook(mchan)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
flog.Info("Start listening for Slack messages")
|
|
|
|
for message := range mchan {
|
|
|
|
texts := strings.Split(message.Text, "\n")
|
|
|
|
for _, text := range texts {
|
|
|
|
flog.Debug("Sending message from " + message.Username + " to " + message.Channel)
|
2016-09-18 19:21:15 +02:00
|
|
|
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()}
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
|
2016-09-05 16:34:37 +02:00
|
|
|
for msg := range b.rtm.IncomingEvents {
|
|
|
|
switch ev := msg.Data.(type) {
|
|
|
|
case *slack.MessageEvent:
|
|
|
|
flog.Debugf("%#v", ev)
|
|
|
|
channel, err := b.rtm.GetChannelInfo(ev.Channel)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user, err := b.rtm.GetUserInfo(ev.User)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = user.Name
|
|
|
|
m.Channel = channel.Name
|
|
|
|
m.Text = ev.Text
|
|
|
|
mchan <- m
|
|
|
|
case *slack.OutgoingErrorEvent:
|
|
|
|
flog.Debugf("%#v", ev.Error())
|
|
|
|
case *slack.ConnectedEvent:
|
|
|
|
b.channels = ev.Info.Channels
|
|
|
|
case *slack.InvalidAuthEvent:
|
|
|
|
flog.Fatalf("Invalid Token %#v", ev)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleMatterHook(mchan chan *MMMessage) {
|
2016-09-05 16:34:37 +02:00
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
|
|
|
flog.Debugf("receiving from slack %#v", message)
|
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = message.UserName
|
|
|
|
m.Text = message.Text
|
|
|
|
m.Channel = message.ChannelName
|
|
|
|
mchan <- m
|
|
|
|
}
|
|
|
|
}
|