matterbridge/bridge/slack/slack.go

272 lines
6.2 KiB
Go
Raw Normal View History

2016-09-05 16:34:37 +02:00
package bslack
import (
"fmt"
2016-09-05 16:34:37 +02:00
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/matterhook"
log "github.com/Sirupsen/logrus"
"github.com/nlopes/slack"
"regexp"
2016-09-05 16:34:37 +02:00
"strings"
"time"
)
type MMMessage struct {
Text string
Channel string
Username string
2016-11-04 00:05:15 +01:00
Raw *slack.MessageEvent
2016-09-05 16:34:37 +02:00
}
type Bslack struct {
mh *matterhook.Client
sc *slack.Client
Config *config.Protocol
2016-09-05 16:34:37 +02:00
rtm *slack.RTM
Plus bool
Remote chan config.Message
Users []slack.User
2016-11-13 23:06:37 +01:00
Account string
2016-11-04 00:05:15 +01:00
si *slack.Info
2016-09-05 16:34:37 +02:00
channels []slack.Channel
}
var flog *log.Entry
var protocol = "slack"
2016-09-05 16:34:37 +02:00
func init() {
flog = log.WithFields(log.Fields{"module": protocol})
2016-09-05 16:34:37 +02:00
}
2016-11-13 23:06:37 +01:00
func New(cfg config.Protocol, account string, c chan config.Message) *Bslack {
b := &Bslack{}
2016-11-04 00:05:15 +01:00
b.Config = &cfg
2016-09-05 16:34:37 +02:00
b.Remote = c
2016-11-13 23:06:37 +01:00
b.Account = account
2016-09-05 16:34:37 +02:00
return b
}
func (b *Bslack) Command(cmd string) string {
2016-09-05 16:34:37 +02:00
return ""
}
func (b *Bslack) Connect() error {
flog.Info("Connecting")
if !b.Config.UseAPI {
b.mh = matterhook.New(b.Config.URL,
matterhook.Config{BindAddress: b.Config.BindAddress})
2016-09-05 16:34:37 +02:00
} else {
b.sc = slack.New(b.Config.Token)
2016-10-25 23:29:32 +02:00
b.rtm = b.sc.NewRTM()
go b.rtm.ManageConnection()
2016-09-05 16:34:37 +02:00
}
flog.Info("Connection succeeded")
2016-09-05 16:34:37 +02:00
go b.handleSlack()
return nil
}
func (b *Bslack) Disconnect() error {
return nil
}
func (b *Bslack) JoinChannel(channel string) error {
// we can only join channels using the API
if b.Config.UseAPI {
2016-09-30 23:15:35 +02:00
_, err := b.sc.JoinChannel(channel)
if err != nil {
return err
}
}
return nil
}
func (b *Bslack) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg)
2016-11-13 23:06:37 +01:00
if msg.Account == b.Account {
return nil
2016-09-05 16:34:37 +02:00
}
2016-11-13 23:06:37 +01:00
nick := msg.Username
message := msg.Text
channel := msg.Channel
if b.Config.PrefixMessagesWithNick {
2016-09-05 16:34:37 +02:00
message = nick + " " + message
}
if !b.Config.UseAPI {
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
2016-09-05 16:34:37 +02:00
matterMessage.Channel = channel
matterMessage.UserName = nick
matterMessage.Type = ""
2016-09-05 16:34:37 +02:00
matterMessage.Text = message
err := b.mh.Send(matterMessage)
if err != nil {
flog.Info(err)
return err
}
return nil
}
schannel, err := b.getChannelByName(channel)
if err != nil {
return err
}
np := slack.NewPostMessageParameters()
if b.Config.PrefixMessagesWithNick == true {
np.AsUser = true
}
np.Username = nick
np.IconURL = config.GetIconURL(&msg, b.Config)
if msg.Avatar != "" {
np.IconURL = msg.Avatar
}
b.sc.PostMessage(schannel.ID, message, np)
/*
newmsg := b.rtm.NewOutgoingMessage(message, schannel.ID)
b.rtm.SendMessage(newmsg)
*/
2016-09-05 16:34:37 +02:00
return nil
}
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
}
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
2016-09-05 16:34:37 +02:00
if b.channels == nil {
2016-11-13 23:06:37 +01:00
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, name)
2016-09-05 16:34:37 +02:00
}
for _, channel := range b.channels {
if channel.Name == name {
return &channel, nil
2016-09-05 16:34:37 +02:00
}
}
2016-11-13 23:06:37 +01:00
return nil, fmt.Errorf("%s: channel %s not found", b.Account, name)
2016-09-05 16:34:37 +02:00
}
func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
if b.channels == nil {
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, ID)
}
for _, channel := range b.channels {
if channel.ID == ID {
return &channel, nil
}
}
return nil, fmt.Errorf("%s: channel %s not found", b.Account, ID)
}
func (b *Bslack) handleSlack() {
flog.Debugf("Choosing API based slack connection: %t", b.Config.UseAPI)
2016-09-05 16:34:37 +02:00
mchan := make(chan *MMMessage)
if b.Config.UseAPI {
2016-09-05 16:34:37 +02:00
go b.handleSlackClient(mchan)
} else {
go b.handleMatterHook(mchan)
}
time.Sleep(time.Second)
flog.Debug("Start listening for Slack messages")
2016-09-05 16:34:37 +02:00
for message := range mchan {
2016-11-04 00:05:15 +01:00
// do not send messages from ourself
if b.Config.UseAPI && message.Username == b.si.User.Name {
2016-11-04 00:05:15 +01:00
continue
}
2016-09-05 16:34:37 +02:00
texts := strings.Split(message.Text, "\n")
for _, text := range texts {
2016-11-13 23:06:37 +01:00
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account)
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Account: b.Account, Avatar: b.getAvatar(message.Username)}
2016-09-05 16:34:37 +02:00
}
}
}
func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
count := 0
2016-09-05 16:34:37 +02:00
for msg := range b.rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
// ignore first message
if count > 0 {
flog.Debugf("Receiving from slackclient %#v", ev)
// use our own func because rtm.GetChannelInfo doesn't work for private channels
channel, err := b.getChannelByID(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-04 00:05:15 +01:00
m.Raw = ev
m.Text = b.replaceMention(m.Text)
mchan <- m
2016-09-05 16:34:37 +02:00
}
count++
2016-09-05 16:34:37 +02:00
case *slack.OutgoingErrorEvent:
flog.Debugf("%#v", ev.Error())
case *slack.ChannelJoinedEvent:
b.Users, _ = b.sc.GetUsers()
2016-09-05 16:34:37 +02:00
case *slack.ConnectedEvent:
b.channels = ev.Info.Channels
2016-11-04 00:05:15 +01:00
b.si = ev.Info
b.Users, _ = b.sc.GetUsers()
// add private channels
groups, _ := b.sc.GetGroups(true)
for _, g := range groups {
channel := new(slack.Channel)
channel.ID = g.ID
channel.Name = g.Name
b.channels = append(b.channels, *channel)
}
2016-09-05 16:34:37 +02:00
case *slack.InvalidAuthEvent:
flog.Fatalf("Invalid Token %#v", ev)
default:
}
}
}
func (b *Bslack) handleMatterHook(mchan chan *MMMessage) {
2016-09-05 16:34:37 +02:00
for {
message := b.mh.Receive()
flog.Debugf("receiving from matterhook (slack) %#v", message)
2016-09-05 16:34:37 +02:00
m := &MMMessage{}
m.Username = message.UserName
m.Text = message.Text
m.Text = b.replaceMention(m.Text)
2016-09-05 16:34:37 +02:00
m.Channel = message.ChannelName
if m.Username == "slackbot" {
continue
}
2016-09-05 16:34:37 +02:00
mchan <- m
}
}
func (b *Bslack) userName(id string) string {
for _, u := range b.Users {
if u.ID == id {
return u.Name
}
}
return ""
}
func (b *Bslack) replaceMention(text string) string {
results := regexp.MustCompile(`<@([a-zA-z0-9]+)>`).FindAllStringSubmatch(text, -1)
for _, r := range results {
text = strings.Replace(text, "<@"+r[1]+">", "@"+b.userName(r[1]), -1)
}
return text
}