2018-10-12 23:16:34 +02:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nlopes/slack"
|
|
|
|
)
|
|
|
|
|
2018-10-13 01:02:14 +02:00
|
|
|
func (b *Bslack) getUsername(id string) string {
|
|
|
|
for _, u := range b.users {
|
2018-10-12 23:16:34 +02:00
|
|
|
if u.ID == id {
|
|
|
|
if u.Profile.DisplayName != "" {
|
|
|
|
return u.Profile.DisplayName
|
|
|
|
}
|
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
b.Log.Warnf("Could not find user with ID '%s'", id)
|
2018-10-12 23:16:34 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getAvatar(userid string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, u := range b.users {
|
|
|
|
if userid == u.ID {
|
|
|
|
return u.Profile.Image48
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannel(channel string) (*slack.Channel, error) {
|
|
|
|
if strings.HasPrefix(channel, "ID:") {
|
|
|
|
return b.getChannelByID(strings.TrimPrefix(channel, "ID:"))
|
|
|
|
}
|
|
|
|
return b.getChannelByName(channel)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
|
|
|
|
if b.channels == nil {
|
|
|
|
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, name)
|
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.Name == name {
|
|
|
|
return &channel, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.Account, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:02:14 +02:00
|
|
|
var (
|
|
|
|
mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`)
|
|
|
|
channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`)
|
|
|
|
variableRE = regexp.MustCompile(`<!((?:subteam\^)?[a-zA-Z0-9]+)(?:\|@?(.+?))?>`)
|
|
|
|
urlRE = regexp.MustCompile(`<(.*?)(\|.*?)?>`)
|
|
|
|
)
|
2018-10-12 23:16:34 +02:00
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceMention(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
replaceFunc := func(match string) string {
|
|
|
|
userID := strings.Trim(match, "@<>")
|
|
|
|
if username := b.getUsername(userID); userID != "" {
|
|
|
|
return "@" + username
|
|
|
|
}
|
|
|
|
return match
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
return mentionRE.ReplaceAllStringFunc(text, replaceFunc)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceChannel(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range channelRE.FindAllStringSubmatch(text, -1) {
|
|
|
|
text = strings.Replace(text, r[0], "#"+r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#variables
|
|
|
|
func (b *Bslack) replaceVariable(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range variableRE.FindAllStringSubmatch(text, -1) {
|
2018-10-12 23:16:34 +02:00
|
|
|
if r[2] != "" {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[2], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
} else {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_urls
|
|
|
|
func (b *Bslack) replaceURL(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range urlRE.FindAllStringSubmatch(text, -1) {
|
2018-10-12 23:16:34 +02:00
|
|
|
if len(strings.TrimSpace(r[2])) == 1 { // A display text separator was found, but the text was blank
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "", 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
} else {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|