2016-09-05 14:34:37 +00:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2016-10-08 19:57:03 +00:00
|
|
|
"fmt"
|
2016-09-05 14:34:37 +00:00
|
|
|
"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-11-03 23:05:15 +00:00
|
|
|
Raw *slack.MessageEvent
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
type Bslack struct {
|
2016-09-19 22:13:57 +00:00
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
2016-09-18 17:21:15 +00:00
|
|
|
Config *config.Protocol
|
2016-09-05 14:34:37 +00:00
|
|
|
rtm *slack.RTM
|
|
|
|
Plus bool
|
|
|
|
Remote chan config.Message
|
2016-11-05 23:46:32 +00:00
|
|
|
Users []slack.User
|
2016-09-18 17:21:15 +00:00
|
|
|
protocol string
|
|
|
|
origin string
|
2016-11-03 23:05:15 +00:00
|
|
|
si *slack.Info
|
2016-09-05 14:34:37 +00:00
|
|
|
channels []slack.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
2016-09-18 17:21:15 +00:00
|
|
|
var protocol = "slack"
|
2016-09-05 14:34:37 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-09-18 17:21:15 +00:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 23:05:15 +00:00
|
|
|
func New(cfg config.Protocol, origin string, c chan config.Message) *Bslack {
|
2016-09-18 17:21:15 +00:00
|
|
|
b := &Bslack{}
|
2016-11-03 23:05:15 +00:00
|
|
|
b.Config = &cfg
|
2016-09-05 14:34:37 +00:00
|
|
|
b.Remote = c
|
2016-09-18 17:21:15 +00:00
|
|
|
b.protocol = protocol
|
|
|
|
b.origin = origin
|
2016-09-05 14:34:37 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) Command(cmd string) string {
|
2016-09-05 14:34:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) Connect() error {
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Info("Connecting")
|
|
|
|
if !b.Config.UseAPI {
|
2016-09-18 17:21:15 +00:00
|
|
|
b.mh = matterhook.New(b.Config.URL,
|
|
|
|
matterhook.Config{BindAddress: b.Config.BindAddress})
|
2016-09-05 14:34:37 +00:00
|
|
|
} else {
|
2016-09-18 17:21:15 +00:00
|
|
|
b.sc = slack.New(b.Config.Token)
|
2016-10-25 21:29:32 +00:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Info("Connection succeeded")
|
2016-09-05 14:34:37 +00:00
|
|
|
go b.handleSlack()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) FullOrigin() string {
|
|
|
|
return b.protocol + "." + b.origin
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) JoinChannel(channel string) error {
|
2016-09-20 10:20:44 +00:00
|
|
|
// we can only join channels using the API
|
|
|
|
if b.Config.UseAPI {
|
2016-09-30 21:15:35 +00:00
|
|
|
_, err := b.sc.JoinChannel(channel)
|
|
|
|
if err != nil {
|
2016-09-20 10:20:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-18 17:21:15 +00:00
|
|
|
}
|
|
|
|
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-19 22:13:57 +00:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2016-11-05 00:11:28 +00:00
|
|
|
if msg.FullOrigin == b.FullOrigin() {
|
|
|
|
return nil
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
2016-11-05 00:11:28 +00:00
|
|
|
nick := config.GetNick(&msg, b.Config)
|
|
|
|
message := msg.Text
|
|
|
|
channel := msg.Channel
|
2016-09-18 17:21:15 +00:00
|
|
|
if b.Config.PrefixMessagesWithNick {
|
2016-09-05 14:34:37 +00:00
|
|
|
message = nick + " " + message
|
|
|
|
}
|
2016-09-19 22:13:57 +00:00
|
|
|
if !b.Config.UseAPI {
|
2016-09-18 17:21:15 +00:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
2016-09-05 14:34:37 +00:00
|
|
|
matterMessage.Channel = channel
|
|
|
|
matterMessage.UserName = nick
|
2016-11-05 00:11:28 +00:00
|
|
|
matterMessage.Type = ""
|
2016-09-05 14:34:37 +00:00
|
|
|
matterMessage.Text = message
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
|
|
|
flog.Info(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-08 19:57:03 +00:00
|
|
|
schannel, err := b.getChannelByName(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-05 00:11:28 +00:00
|
|
|
np := slack.NewPostMessageParameters()
|
|
|
|
if b.Config.PrefixMessagesWithNick == true {
|
|
|
|
np.AsUser = true
|
|
|
|
}
|
|
|
|
np.Username = nick
|
|
|
|
np.IconURL = config.GetIconURL(&msg, b.Config)
|
2016-11-05 23:46:32 +00:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
np.IconURL = msg.Avatar
|
|
|
|
}
|
2016-11-05 00:11:28 +00:00
|
|
|
b.sc.PostMessage(schannel.ID, message, np)
|
|
|
|
|
|
|
|
/*
|
|
|
|
newmsg := b.rtm.NewOutgoingMessage(message, schannel.ID)
|
|
|
|
b.rtm.SendMessage(newmsg)
|
|
|
|
*/
|
|
|
|
|
2016-09-05 14:34:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:46:32 +00:00
|
|
|
func (b *Bslack) getAvatar(user string) string {
|
|
|
|
var avatar string
|
|
|
|
if b.Users != nil {
|
|
|
|
for _, u := range b.Users {
|
|
|
|
if user == u.Name {
|
|
|
|
return u.Profile.Image48
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return avatar
|
|
|
|
}
|
|
|
|
|
2016-10-08 19:57:03 +00:00
|
|
|
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
|
2016-09-05 14:34:37 +00:00
|
|
|
if b.channels == nil {
|
2016-10-08 19:57:03 +00:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.FullOrigin(), name)
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.Name == name {
|
2016-10-08 19:57:03 +00:00
|
|
|
return &channel, nil
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 19:57:03 +00:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.FullOrigin(), name)
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) handleSlack() {
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Debugf("Choosing API based slack connection: %t", b.Config.UseAPI)
|
2016-09-05 14:34:37 +00:00
|
|
|
mchan := make(chan *MMMessage)
|
2016-09-19 22:13:57 +00:00
|
|
|
if b.Config.UseAPI {
|
2016-09-05 14:34:37 +00:00
|
|
|
go b.handleSlackClient(mchan)
|
|
|
|
} else {
|
|
|
|
go b.handleMatterHook(mchan)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Debug("Start listening for Slack messages")
|
2016-09-05 14:34:37 +00:00
|
|
|
for message := range mchan {
|
2016-11-03 23:05:15 +00:00
|
|
|
// do not send messages from ourself
|
|
|
|
if message.Username == b.si.User.Name {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-05 14:34:37 +00:00
|
|
|
texts := strings.Split(message.Text, "\n")
|
|
|
|
for _, text := range texts {
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.FullOrigin())
|
2016-11-05 23:46:32 +00:00
|
|
|
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: b.getAvatar(message.Username)}
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
|
2016-10-29 19:05:33 +00:00
|
|
|
count := 0
|
2016-09-05 14:34:37 +00:00
|
|
|
for msg := range b.rtm.IncomingEvents {
|
|
|
|
switch ev := msg.Data.(type) {
|
|
|
|
case *slack.MessageEvent:
|
2016-10-29 19:05:33 +00:00
|
|
|
// ignore first message
|
|
|
|
if count > 0 {
|
|
|
|
flog.Debugf("Receiving from slackclient %#v", ev)
|
|
|
|
//ev.ReplyTo
|
|
|
|
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
|
2016-11-03 23:05:15 +00:00
|
|
|
m.Raw = ev
|
2016-10-29 19:05:33 +00:00
|
|
|
mchan <- m
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
2016-10-29 19:05:33 +00:00
|
|
|
count++
|
2016-09-05 14:34:37 +00:00
|
|
|
case *slack.OutgoingErrorEvent:
|
|
|
|
flog.Debugf("%#v", ev.Error())
|
|
|
|
case *slack.ConnectedEvent:
|
|
|
|
b.channels = ev.Info.Channels
|
2016-11-03 23:05:15 +00:00
|
|
|
b.si = ev.Info
|
2016-11-05 23:46:32 +00:00
|
|
|
b.Users, _ = b.sc.GetUsers()
|
2016-09-05 14:34:37 +00:00
|
|
|
case *slack.InvalidAuthEvent:
|
|
|
|
flog.Fatalf("Invalid Token %#v", ev)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) handleMatterHook(mchan chan *MMMessage) {
|
2016-09-05 14:34:37 +00:00
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
2016-09-19 22:13:57 +00:00
|
|
|
flog.Debugf("receiving from matterhook (slack) %#v", message)
|
2016-09-05 14:34:37 +00:00
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = message.UserName
|
|
|
|
m.Text = message.Text
|
|
|
|
m.Channel = message.ChannelName
|
|
|
|
mchan <- m
|
|
|
|
}
|
|
|
|
}
|