2016-09-05 07:34:37 -07:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2017-07-15 07:49:47 -07:00
|
|
|
"errors"
|
2018-10-12 16:02:14 -07:00
|
|
|
"fmt"
|
2016-09-05 07:34:37 -07:00
|
|
|
"strings"
|
2018-03-04 14:52:14 -08:00
|
|
|
"sync"
|
2018-03-22 14:28:27 -07:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2018-08-17 15:12:05 -07:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2018-03-22 14:28:27 -07:00
|
|
|
"github.com/nlopes/slack"
|
2018-05-27 12:50:00 -07:00
|
|
|
"github.com/rs/xid"
|
2016-09-05 07:34:37 -07:00
|
|
|
)
|
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
type Bslack struct {
|
2018-07-13 14:23:11 -07:00
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
|
|
|
rtm *slack.RTM
|
2018-10-12 16:02:14 -07:00
|
|
|
users []slack.User
|
2018-07-13 14:23:11 -07:00
|
|
|
si *slack.Info
|
|
|
|
channels []slack.Channel
|
2018-08-17 15:12:05 -07:00
|
|
|
cache *lru.Cache
|
2018-10-12 16:02:14 -07:00
|
|
|
useChannelID bool
|
2018-07-13 14:23:11 -07:00
|
|
|
uuid string
|
2018-03-04 14:52:14 -08:00
|
|
|
*bridge.Config
|
|
|
|
sync.RWMutex
|
2016-09-05 07:34:37 -07:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:02:14 -07:00
|
|
|
const (
|
|
|
|
sChannelJoin = "channel_join"
|
|
|
|
sChannelLeave = "channel_leave"
|
|
|
|
sMessageDeleted = "message_deleted"
|
|
|
|
sSlackAttachment = "slack_attachment"
|
|
|
|
sPinnedItem = "pinned_item"
|
|
|
|
sUnpinnedItem = "unpinned_item"
|
|
|
|
sChannelTopic = "channel_topic"
|
|
|
|
sChannelPurpose = "channel_purpose"
|
|
|
|
sFileComment = "file_comment"
|
|
|
|
sMeMessage = "me_message"
|
|
|
|
sUserTyping = "user_typing"
|
|
|
|
sLatencyReport = "latency_report"
|
|
|
|
sSystemUser = "system"
|
|
|
|
|
|
|
|
tokenConfig = "Token"
|
|
|
|
incomingWebhookConfig = "WebhookBindAddress"
|
|
|
|
outgoingWebhookConfig = "WebhookURL"
|
|
|
|
skipTLSConfig = "SkipTLSVerify"
|
|
|
|
useNickPrefixConfig = "PrefixMessagesWithNick"
|
|
|
|
editDisableConfig = "EditDisable"
|
|
|
|
editSuffixConfig = "EditSuffix"
|
|
|
|
iconURLConfig = "iconurl"
|
|
|
|
noSendJoinConfig = "nosendjoinpart"
|
|
|
|
)
|
2016-09-05 07:34:37 -07:00
|
|
|
|
2018-03-04 14:52:14 -08:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2018-10-12 16:02:14 -07:00
|
|
|
newCache, err := lru.New(5000)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
|
|
|
|
}
|
|
|
|
b := &Bslack{
|
|
|
|
Config: cfg,
|
|
|
|
uuid: xid.New().String(),
|
|
|
|
cache: newCache,
|
|
|
|
}
|
2018-08-17 15:12:05 -07:00
|
|
|
return b
|
2016-09-05 07:34:37 -07:00
|
|
|
}
|
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
func (b *Bslack) Command(cmd string) string {
|
2016-09-05 07:34:37 -07:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
func (b *Bslack) Connect() error {
|
2018-03-04 14:52:14 -08:00
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetString(incomingWebhookConfig) != "" {
|
|
|
|
if b.GetString(outgoingWebhookConfig) != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
|
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
|
|
BindAddress: b.GetString(incomingWebhookConfig),
|
|
|
|
})
|
|
|
|
} else if b.GetString(tokenConfig) != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using token (sending)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.sc = slack.New(b.GetString(tokenConfig))
|
2017-07-15 07:49:47 -07:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
|
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
|
|
BindAddress: b.GetString(incomingWebhookConfig),
|
|
|
|
})
|
2017-07-15 07:49:47 -07:00
|
|
|
} else {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
|
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
|
|
BindAddress: b.GetString(incomingWebhookConfig),
|
|
|
|
})
|
2017-07-15 07:49:47 -07:00
|
|
|
}
|
|
|
|
go b.handleSlack()
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetString(outgoingWebhookConfig) != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using webhookurl (sending)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
|
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
|
|
DisableServer: true,
|
|
|
|
})
|
|
|
|
if b.GetString(tokenConfig) != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using token (receiving)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.sc = slack.New(b.GetString(tokenConfig))
|
2017-07-15 07:49:47 -07:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
|
|
|
go b.handleSlack()
|
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
} else if b.GetString(tokenConfig) != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting using token (sending and receiving)")
|
2018-10-12 16:02:14 -07:00
|
|
|
b.sc = slack.New(b.GetString(tokenConfig))
|
2016-10-25 14:29:32 -07:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2017-07-15 07:49:47 -07:00
|
|
|
go b.handleSlack()
|
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
2018-02-24 14:35:53 -08:00
|
|
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
2016-09-05 07:34:37 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:12:02 -08:00
|
|
|
func (b *Bslack) Disconnect() error {
|
2018-03-04 14:52:14 -08:00
|
|
|
return b.rtm.Disconnect()
|
2017-02-14 12:12:02 -08:00
|
|
|
}
|
|
|
|
|
2017-08-12 05:51:41 -07:00
|
|
|
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
2018-07-13 14:23:11 -07:00
|
|
|
// use ID:channelid and resolve it to the actual name
|
|
|
|
idcheck := strings.Split(channel.Name, "ID:")
|
|
|
|
if len(idcheck) > 1 {
|
2018-10-12 16:02:14 -07:00
|
|
|
b.useChannelID = true
|
2018-07-13 14:23:11 -07:00
|
|
|
ch, err := b.sc.GetChannelInfo(idcheck[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channel.Name = ch.Name
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:20:44 -07:00
|
|
|
// we can only join channels using the API
|
2018-01-02 05:31:44 -08:00
|
|
|
if b.sc != nil {
|
2018-10-12 16:02:14 -07:00
|
|
|
if strings.HasPrefix(b.GetString(tokenConfig), "xoxb") {
|
2017-03-27 11:15:05 -07:00
|
|
|
// TODO check if bot has already joined channel
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-12 05:51:41 -07:00
|
|
|
_, err := b.sc.JoinChannel(channel.Name)
|
2016-09-30 14:15:35 -07:00
|
|
|
if err != nil {
|
2018-03-12 13:14:13 -07:00
|
|
|
switch err.Error() {
|
|
|
|
case "name_taken", "restricted_action":
|
|
|
|
case "default":
|
|
|
|
{
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 09:01:24 -07:00
|
|
|
}
|
2016-09-20 03:20:44 -07:00
|
|
|
}
|
2016-09-18 10:21:15 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 13:59:37 -07:00
|
|
|
func (b *Bslack) Send(msg config.Message) (string, error) {
|
2018-02-28 13:23:29 -08:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-24 14:35:53 -08:00
|
|
|
|
|
|
|
// Make a action /me of the message
|
2017-07-30 08:48:23 -07:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
|
|
|
|
// Use webhook to send the message
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetString(outgoingWebhookConfig) != "" {
|
2018-02-24 14:35:53 -08:00
|
|
|
return b.sendWebhook(msg)
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:02:14 -07:00
|
|
|
channelInfo, err := b.getChannel(msg.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not send message: %v", err)
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
|
|
|
|
// Delete message
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
// some protocols echo deletes, but with empty ID
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
2018-02-02 16:11:11 -08:00
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
// we get a "slack <ID>", split it
|
|
|
|
ts := strings.Fields(msg.ID)
|
2018-10-12 16:02:14 -07:00
|
|
|
_, _, err = b.sc.DeleteMessage(channelInfo.ID, ts[1])
|
2018-02-24 14:35:53 -08:00
|
|
|
if err != nil {
|
|
|
|
return msg.ID, err
|
2018-02-22 15:49:32 -08:00
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
return msg.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend nick if configured
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
2018-02-24 14:35:53 -08:00
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
2018-02-02 16:11:11 -08:00
|
|
|
|
2018-02-24 14:35:53 -08:00
|
|
|
// Edit message if we have an ID
|
|
|
|
if msg.ID != "" {
|
|
|
|
ts := strings.Fields(msg.ID)
|
2018-10-12 16:02:14 -07:00
|
|
|
_, _, _, err = b.sc.UpdateMessage(channelInfo.ID, ts[1], msg.Text)
|
2016-09-05 07:34:37 -07:00
|
|
|
if err != nil {
|
2018-02-24 14:35:53 -08:00
|
|
|
return msg.ID, err
|
2016-09-05 07:34:37 -07:00
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
return msg.ID, nil
|
2016-10-08 12:57:03 -07:00
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
|
|
|
|
// create slack new post parameters
|
2016-11-04 17:11:28 -07:00
|
|
|
np := slack.NewPostMessageParameters()
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
2016-11-04 17:11:28 -07:00
|
|
|
np.AsUser = true
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
np.Username = msg.Username
|
|
|
|
np.LinkNames = 1 // replace mentions
|
2018-10-12 16:02:14 -07:00
|
|
|
np.IconURL = config.GetIconURL(&msg, b.GetString(iconURLConfig))
|
2016-11-05 16:46:32 -07:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
np.IconURL = msg.Avatar
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
// add a callback ID so we can see we created it
|
2018-05-27 12:50:00 -07:00
|
|
|
np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid})
|
2018-02-24 14:35:53 -08:00
|
|
|
// add file attachments
|
2017-09-18 14:51:27 -07:00
|
|
|
np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...)
|
2018-02-24 14:35:53 -08:00
|
|
|
// add slack attachments (from another slack bridge)
|
2018-10-12 16:02:14 -07:00
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, attach := range msg.Extra[sSlackAttachment] {
|
2018-02-22 15:49:32 -08:00
|
|
|
np.Attachments = append(np.Attachments, attach.([]slack.Attachment)...)
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 14:51:27 -07:00
|
|
|
|
2018-02-24 14:35:53 -08:00
|
|
|
// Upload a file if it exists
|
2017-11-03 15:10:16 -07:00
|
|
|
if msg.Extra != nil {
|
2018-02-02 16:11:11 -08:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-10-12 16:02:14 -07:00
|
|
|
_, _, err = b.sc.PostMessage(channelInfo.ID, rmsg.Username+rmsg.Text, np)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Error(err)
|
|
|
|
}
|
2017-11-03 15:10:16 -07:00
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
// Upload files if necessary (from Slack, Telegram or Mattermost).
|
|
|
|
b.handleUploadFile(&msg, channelInfo.ID)
|
2017-11-03 15:10:16 -07:00
|
|
|
}
|
|
|
|
|
2018-02-24 14:35:53 -08:00
|
|
|
// Post normal message
|
2018-10-12 16:02:14 -07:00
|
|
|
_, id, err := b.sc.PostMessage(channelInfo.ID, msg.Text, np)
|
2017-08-28 11:29:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "slack " + id, nil
|
2016-09-05 07:34:37 -07:00
|
|
|
}
|
|
|
|
|
2018-03-04 14:52:14 -08:00
|
|
|
func (b *Bslack) Reload(cfg *bridge.Config) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:35:21 -07:00
|
|
|
func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment {
|
2018-10-12 16:02:14 -07:00
|
|
|
var attachements []slack.Attachment
|
2017-09-21 13:35:21 -07:00
|
|
|
for _, v := range extra["attachments"] {
|
2017-09-18 15:04:27 -07:00
|
|
|
entry := v.(map[string]interface{})
|
2018-10-12 16:02:14 -07:00
|
|
|
s := slack.Attachment{
|
|
|
|
Fallback: extractStringField(entry, "fallback"),
|
|
|
|
Color: extractStringField(entry, "color"),
|
|
|
|
Pretext: extractStringField(entry, "pretext"),
|
|
|
|
AuthorName: extractStringField(entry, "author_name"),
|
|
|
|
AuthorLink: extractStringField(entry, "author_link"),
|
|
|
|
AuthorIcon: extractStringField(entry, "author_icon"),
|
|
|
|
Title: extractStringField(entry, "title"),
|
|
|
|
TitleLink: extractStringField(entry, "title_link"),
|
|
|
|
Text: extractStringField(entry, "text"),
|
|
|
|
ImageURL: extractStringField(entry, "image_url"),
|
|
|
|
ThumbURL: extractStringField(entry, "thumb_url"),
|
|
|
|
Footer: extractStringField(entry, "footer"),
|
|
|
|
FooterIcon: extractStringField(entry, "footer_icon"),
|
|
|
|
}
|
|
|
|
attachements = append(attachements, s)
|
|
|
|
}
|
|
|
|
return attachements
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractStringField(data map[string]interface{}, field string) string {
|
|
|
|
if rawValue, found := data[field]; found {
|
|
|
|
if value, ok := rawValue.(string); ok {
|
|
|
|
return value
|
|
|
|
}
|
2017-09-18 14:51:27 -07:00
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
return ""
|
2017-09-18 14:51:27 -07:00
|
|
|
}
|
2017-09-21 13:35:21 -07:00
|
|
|
|
2018-02-24 14:35:53 -08:00
|
|
|
// sendWebhook uses the configured WebhookURL to send the message
|
|
|
|
func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
|
|
|
|
// skip events
|
|
|
|
if msg.Event != "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:02:14 -07:00
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
2018-02-24 14:35:53 -08:00
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Extra != nil {
|
|
|
|
// this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-10-12 16:02:14 -07:00
|
|
|
iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig))
|
|
|
|
matterMessage := matterhook.OMessage{
|
|
|
|
IconURL: iconURL,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
UserName: rmsg.Username,
|
|
|
|
Text: rmsg.Text,
|
|
|
|
}
|
|
|
|
if err := b.mh.Send(matterMessage); err != nil {
|
|
|
|
b.Log.Errorf("Failed to send message: %v", err)
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// webhook doesn't support file uploads, so we add the url manually
|
2018-10-12 16:02:14 -07:00
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text += " " + fi.URL
|
2018-02-24 14:35:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have native slack_attachments add them
|
|
|
|
var attachs []slack.Attachment
|
2018-10-12 16:02:14 -07:00
|
|
|
for _, attach := range msg.Extra[sSlackAttachment] {
|
|
|
|
attachs = append(attachs, attach.([]slack.Attachment)...)
|
2018-02-24 14:35:53 -08:00
|
|
|
}
|
|
|
|
|
2018-10-12 16:02:14 -07:00
|
|
|
iconURL := config.GetIconURL(&msg, b.GetString(iconURLConfig))
|
|
|
|
matterMessage := matterhook.OMessage{
|
|
|
|
IconURL: iconURL,
|
|
|
|
Attachments: attachs,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
UserName: msg.Username,
|
|
|
|
Text: msg.Text,
|
|
|
|
}
|
2018-02-24 14:35:53 -08:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
matterMessage.IconURL = msg.Avatar
|
|
|
|
}
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Error(err)
|
2018-02-24 14:35:53 -08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|