2018-10-12 21:16:34 +00:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2018-11-01 20:28:22 +00:00
|
|
|
"time"
|
2018-10-12 21:16:34 +00:00
|
|
|
|
2018-11-07 20:35:59 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2019-03-12 21:52:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-01 19:59:19 +00:00
|
|
|
"github.com/slack-go/slack"
|
2018-10-12 21:16:34 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 20:35:59 +00:00
|
|
|
// populateReceivedMessage shapes the initial Matterbridge message that we will forward to the
|
|
|
|
// router before we apply message-dependent modifications.
|
|
|
|
func (b *Bslack) populateReceivedMessage(ev *slack.MessageEvent) (*config.Message, error) {
|
|
|
|
// Use our own func because rtm.GetChannelInfo doesn't work for private channels.
|
2019-03-15 20:23:09 +00:00
|
|
|
channel, err := b.channels.getChannelByID(ev.Channel)
|
2018-11-07 20:35:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := &config.Message{
|
|
|
|
Text: ev.Text,
|
|
|
|
Channel: channel.Name,
|
|
|
|
Account: b.Account,
|
2018-11-19 20:28:23 +00:00
|
|
|
ID: ev.Timestamp,
|
2018-11-07 20:35:59 +00:00
|
|
|
Extra: make(map[string][]interface{}),
|
|
|
|
ParentID: ev.ThreadTimestamp,
|
2018-11-19 20:28:23 +00:00
|
|
|
Protocol: b.Protocol,
|
2018-11-07 20:35:59 +00:00
|
|
|
}
|
|
|
|
if b.useChannelID {
|
|
|
|
rmsg.Channel = "ID:" + channel.ID
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:07:21 +00:00
|
|
|
// Handle 'edit' messages.
|
|
|
|
if ev.SubMessage != nil && !b.GetBool(editDisableConfig) {
|
2018-11-19 20:28:23 +00:00
|
|
|
rmsg.ID = ev.SubMessage.Timestamp
|
2018-11-08 19:07:21 +00:00
|
|
|
if ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
|
|
|
b.Log.Debugf("SubMessage %#v", ev.SubMessage)
|
|
|
|
rmsg.Text = ev.SubMessage.Text + b.GetString(editSuffixConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:23:50 +00:00
|
|
|
// For edits, only submessage has thread ts.
|
|
|
|
// Ensures edits to threaded messages maintain their prefix hint on the
|
|
|
|
// unthreaded end.
|
|
|
|
if ev.SubMessage != nil {
|
|
|
|
rmsg.ParentID = ev.SubMessage.ThreadTimestamp
|
|
|
|
}
|
|
|
|
|
2018-11-07 20:35:59 +00:00
|
|
|
if err = b.populateMessageWithUserInfo(ev, rmsg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rmsg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) populateMessageWithUserInfo(ev *slack.MessageEvent, rmsg *config.Message) error {
|
|
|
|
if ev.SubType == sMessageDeleted || ev.SubType == sFileComment {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:07:21 +00:00
|
|
|
// First, deal with bot-originating messages but only do so when not using webhooks: we
|
|
|
|
// would not be able to distinguish which bot would be sending them.
|
2018-11-10 21:09:41 +00:00
|
|
|
if err := b.populateMessageWithBotInfo(ev, rmsg); err != nil {
|
|
|
|
return err
|
2018-11-07 20:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:07:21 +00:00
|
|
|
// Second, deal with "real" users if we have the necessary information.
|
|
|
|
var userID string
|
2018-11-08 21:01:29 +00:00
|
|
|
switch {
|
|
|
|
case ev.User != "":
|
2018-11-08 19:07:21 +00:00
|
|
|
userID = ev.User
|
2018-11-08 21:01:29 +00:00
|
|
|
case ev.SubMessage != nil && ev.SubMessage.User != "":
|
2018-11-08 19:07:21 +00:00
|
|
|
userID = ev.SubMessage.User
|
2018-11-08 21:01:29 +00:00
|
|
|
default:
|
2018-11-08 19:07:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 20:23:09 +00:00
|
|
|
user := b.users.getUser(userID)
|
2018-11-08 19:07:21 +00:00
|
|
|
if user == nil {
|
|
|
|
return fmt.Errorf("could not find information for user with id %s", ev.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg.UserID = user.ID
|
|
|
|
rmsg.Username = user.Name
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
rmsg.Username = user.Profile.DisplayName
|
2018-11-07 20:35:59 +00:00
|
|
|
}
|
2022-03-26 19:52:24 +00:00
|
|
|
if b.GetBool("UseFullName") && user.Profile.RealName != "" {
|
|
|
|
rmsg.Username = user.Profile.RealName
|
|
|
|
}
|
2018-11-07 20:35:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 21:09:41 +00:00
|
|
|
func (b *Bslack) populateMessageWithBotInfo(ev *slack.MessageEvent, rmsg *config.Message) error {
|
|
|
|
if ev.BotID == "" || b.GetString(outgoingWebhookConfig) != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var bot *slack.Bot
|
|
|
|
for {
|
2024-05-23 21:44:31 +00:00
|
|
|
bot, err = b.rtm.GetBotInfo(slack.GetBotInfoParameters{
|
|
|
|
Bot: ev.BotID,
|
|
|
|
})
|
2018-11-10 21:09:41 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:52:36 +00:00
|
|
|
if err = handleRateLimit(b.Log, err); err != nil {
|
2018-11-10 21:09:41 +00:00
|
|
|
b.Log.Errorf("Could not retrieve bot information: %#v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-11 23:27:55 +00:00
|
|
|
b.Log.Debugf("Found bot %#v", bot)
|
2018-11-10 21:09:41 +00:00
|
|
|
|
2018-12-13 19:49:14 +00:00
|
|
|
if bot.Name != "" {
|
2018-11-10 21:09:41 +00:00
|
|
|
rmsg.Username = bot.Name
|
|
|
|
if ev.Username != "" {
|
|
|
|
rmsg.Username = ev.Username
|
|
|
|
}
|
|
|
|
rmsg.UserID = bot.ID
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:02:14 +00:00
|
|
|
var (
|
2018-11-26 09:47:04 +00:00
|
|
|
mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`)
|
|
|
|
channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`)
|
|
|
|
variableRE = regexp.MustCompile(`<!((?:subteam\^)?[a-zA-Z0-9]+)(?:\|@?(.+?))?>`)
|
2022-05-09 20:56:19 +00:00
|
|
|
urlRE = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
|
2019-01-04 19:32:58 +00:00
|
|
|
codeFenceRE = regexp.MustCompile(`(?m)^` + "```" + `\w+$`)
|
2018-11-26 09:47:04 +00:00
|
|
|
topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`)
|
2018-10-12 23:02:14 +00:00
|
|
|
)
|
2018-10-12 21:16:34 +00:00
|
|
|
|
2018-11-26 09:47:04 +00:00
|
|
|
func (b *Bslack) extractTopicOrPurpose(text string) (string, string) {
|
|
|
|
r := topicOrPurposeRE.FindStringSubmatch(text)
|
|
|
|
if len(r) == 5 {
|
|
|
|
action, updateType, extracted := r[2], r[3], r[4]
|
|
|
|
switch action {
|
|
|
|
case "set":
|
|
|
|
return updateType, extracted
|
|
|
|
case "cleared":
|
|
|
|
return updateType, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Log.Warnf("Encountered channel topic or purpose change message with unexpected format: %s", text)
|
|
|
|
return "unknown", ""
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:16:34 +00:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceMention(text string) string {
|
2018-10-12 23:02:14 +00:00
|
|
|
replaceFunc := func(match string) string {
|
|
|
|
userID := strings.Trim(match, "@<>")
|
2019-03-15 20:23:09 +00:00
|
|
|
if username := b.users.getUsername(userID); userID != "" {
|
2018-10-12 23:02:14 +00:00
|
|
|
return "@" + username
|
|
|
|
}
|
|
|
|
return match
|
2018-10-12 21:16:34 +00:00
|
|
|
}
|
2018-10-12 23:02:14 +00:00
|
|
|
return mentionRE.ReplaceAllStringFunc(text, replaceFunc)
|
2018-10-12 21:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceChannel(text string) string {
|
2018-10-12 23:02:14 +00:00
|
|
|
for _, r := range channelRE.FindAllStringSubmatch(text, -1) {
|
|
|
|
text = strings.Replace(text, r[0], "#"+r[1], 1)
|
2018-10-12 21:16:34 +00:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#variables
|
|
|
|
func (b *Bslack) replaceVariable(text string) string {
|
2018-10-12 23:02:14 +00:00
|
|
|
for _, r := range variableRE.FindAllStringSubmatch(text, -1) {
|
2018-10-12 21:16:34 +00:00
|
|
|
if r[2] != "" {
|
2018-10-12 23:02:14 +00:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[2], 1)
|
2018-10-12 21:16:34 +00:00
|
|
|
} else {
|
2018-10-12 23:02:14 +00:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[1], 1)
|
2018-10-12 21:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_urls
|
|
|
|
func (b *Bslack) replaceURL(text string) string {
|
2022-05-09 20:56:19 +00:00
|
|
|
return urlRE.ReplaceAllString(text, "[${2}](${1})")
|
2018-10-12 21:16:34 +00:00
|
|
|
}
|
2018-11-10 21:09:41 +00:00
|
|
|
|
2019-10-26 23:10:59 +00:00
|
|
|
func (b *Bslack) replaceb0rkedMarkDown(text string) string {
|
|
|
|
// taken from https://github.com/mattermost/mattermost-server/blob/master/app/slackimport.go
|
|
|
|
//
|
|
|
|
regexReplaceAllString := []struct {
|
|
|
|
regex *regexp.Regexp
|
|
|
|
rpl string
|
|
|
|
}{
|
|
|
|
// bold
|
|
|
|
{
|
|
|
|
regexp.MustCompile(`(^|[\s.;,])\*(\S[^*\n]+)\*`),
|
|
|
|
"$1**$2**",
|
|
|
|
},
|
|
|
|
// strikethrough
|
|
|
|
{
|
|
|
|
regexp.MustCompile(`(^|[\s.;,])\~(\S[^~\n]+)\~`),
|
|
|
|
"$1~~$2~~",
|
|
|
|
},
|
|
|
|
// single paragraph blockquote
|
|
|
|
// Slack converts > character to >
|
|
|
|
{
|
|
|
|
regexp.MustCompile(`(?sm)^>`),
|
|
|
|
">",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, rule := range regexReplaceAllString {
|
|
|
|
text = rule.regex.ReplaceAllString(text, rule.rpl)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2019-01-04 19:32:58 +00:00
|
|
|
func (b *Bslack) replaceCodeFence(text string) string {
|
|
|
|
return codeFenceRE.ReplaceAllString(text, "```")
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:35:31 +00:00
|
|
|
// getUsersInConversation returns an array of userIDs that are members of channelID
|
|
|
|
func (b *Bslack) getUsersInConversation(channelID string) ([]string, error) {
|
|
|
|
channelMembers := []string{}
|
|
|
|
for {
|
|
|
|
queryParams := &slack.GetUsersInConversationParameters{
|
|
|
|
ChannelID: channelID,
|
|
|
|
}
|
|
|
|
|
|
|
|
members, nextCursor, err := b.sc.GetUsersInConversation(queryParams)
|
|
|
|
if err != nil {
|
2019-03-12 21:52:36 +00:00
|
|
|
if err = handleRateLimit(b.Log, err); err != nil {
|
2019-01-18 17:35:31 +00:00
|
|
|
return channelMembers, fmt.Errorf("Could not retrieve users in channels: %#v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
channelMembers = append(channelMembers, members...)
|
|
|
|
|
|
|
|
if nextCursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
queryParams.Cursor = nextCursor
|
|
|
|
}
|
|
|
|
return channelMembers, nil
|
|
|
|
}
|
2019-03-12 21:52:36 +00:00
|
|
|
|
|
|
|
func handleRateLimit(log *logrus.Entry, err error) error {
|
|
|
|
rateLimit, ok := err.(*slack.RateLimitedError)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("Rate-limited by Slack. Sleeping for %v", rateLimit.RetryAfter)
|
|
|
|
time.Sleep(rateLimit.RetryAfter)
|
|
|
|
return nil
|
|
|
|
}
|