2016-09-05 14:34:37 +00:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2017-09-21 20:35:21 +00:00
|
|
|
"bytes"
|
2017-07-15 14:49:47 +00:00
|
|
|
"errors"
|
2016-10-08 19:57:03 +00:00
|
|
|
"fmt"
|
2017-07-09 12:26:35 +00:00
|
|
|
"html"
|
2016-11-20 21:40:09 +00:00
|
|
|
"regexp"
|
2016-09-05 14:34:37 +00:00
|
|
|
"strings"
|
2018-03-04 22:52:14 +00:00
|
|
|
"sync"
|
2016-09-05 14:34:37 +00:00
|
|
|
"time"
|
2018-03-22 21:28:27 +00:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
|
|
|
"github.com/nlopes/slack"
|
2018-05-27 19:50:00 +00:00
|
|
|
"github.com/rs/xid"
|
2016-09-05 14:34:37 +00:00
|
|
|
)
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
type Bslack struct {
|
2018-07-13 21:23:11 +00:00
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
|
|
|
rtm *slack.RTM
|
|
|
|
Users []slack.User
|
|
|
|
Usergroups []slack.UserGroup
|
|
|
|
si *slack.Info
|
|
|
|
channels []slack.Channel
|
|
|
|
UseChannelID bool
|
|
|
|
uuid string
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
|
|
|
sync.RWMutex
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
const messageDeleted = "message_deleted"
|
2016-09-05 14:34:37 +00:00
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2018-05-27 19:50:00 +00:00
|
|
|
return &Bslack{Config: cfg, uuid: xid.New().String()}
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-04 22:52:14 +00:00
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
|
|
|
if b.GetString("WebhookBindAddress") != "" {
|
|
|
|
if b.GetString("WebhookURL") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
|
|
|
BindAddress: b.GetString("WebhookBindAddress")})
|
|
|
|
} else if b.GetString("Token") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using token (sending)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.sc = slack.New(b.GetString("Token"))
|
2017-07-15 14:49:47 +00:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
|
|
|
BindAddress: b.GetString("WebhookBindAddress")})
|
2017-07-15 14:49:47 +00:00
|
|
|
} else {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
|
|
|
BindAddress: b.GetString("WebhookBindAddress")})
|
2017-07-15 14:49:47 +00:00
|
|
|
}
|
|
|
|
go b.handleSlack()
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using webhookurl (sending)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
2017-07-15 14:49:47 +00:00
|
|
|
DisableServer: true})
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("Token") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using token (receiving)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.sc = slack.New(b.GetString("Token"))
|
2017-07-15 14:49:47 +00:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
|
|
|
go b.handleSlack()
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
} else if b.GetString("Token") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using token (sending and receiving)")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.sc = slack.New(b.GetString("Token"))
|
2016-10-25 21:29:32 +00:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2017-07-15 14:49:47 +00:00
|
|
|
go b.handleSlack()
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" && b.GetString("Token") == "" {
|
2018-02-24 22:35:53 +00:00
|
|
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Bslack) Disconnect() error {
|
2018-03-04 22:52:14 +00:00
|
|
|
return b.rtm.Disconnect()
|
2017-02-14 20:12:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
2018-07-13 21:23:11 +00:00
|
|
|
// use ID:channelid and resolve it to the actual name
|
|
|
|
idcheck := strings.Split(channel.Name, "ID:")
|
|
|
|
if len(idcheck) > 1 {
|
|
|
|
b.UseChannelID = true
|
|
|
|
ch, err := b.sc.GetChannelInfo(idcheck[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channel.Name = ch.Name
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 10:20:44 +00:00
|
|
|
// we can only join channels using the API
|
2018-01-02 13:31:44 +00:00
|
|
|
if b.sc != nil {
|
2018-03-04 22:52:14 +00:00
|
|
|
if strings.HasPrefix(b.GetString("Token"), "xoxb") {
|
2017-03-27 18:15:05 +00:00
|
|
|
// TODO check if bot has already joined channel
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-12 12:51:41 +00:00
|
|
|
_, err := b.sc.JoinChannel(channel.Name)
|
2016-09-30 21:15:35 +00:00
|
|
|
if err != nil {
|
2018-03-12 20:14:13 +00:00
|
|
|
switch err.Error() {
|
|
|
|
case "name_taken", "restricted_action":
|
|
|
|
case "default":
|
|
|
|
{
|
|
|
|
return err
|
|
|
|
}
|
2017-04-17 16:01:24 +00:00
|
|
|
}
|
2016-09-20 10:20:44 +00:00
|
|
|
}
|
2016-09-18 17:21:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Bslack) Send(msg config.Message) (string, error) {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-24 22:35:53 +00:00
|
|
|
|
|
|
|
// Make a action /me of the message
|
2017-07-30 15:48:23 +00:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
|
|
|
|
// Use webhook to send the message
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
2018-02-24 22:35:53 +00:00
|
|
|
return b.sendWebhook(msg)
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:23:11 +00:00
|
|
|
channelID := b.getChannelID(msg.Channel)
|
2018-02-24 22:35:53 +00: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-03 00:11:11 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
// we get a "slack <ID>", split it
|
|
|
|
ts := strings.Fields(msg.ID)
|
2018-07-13 21:23:11 +00:00
|
|
|
_, _, err := b.sc.DeleteMessage(channelID, ts[1])
|
2018-02-24 22:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return msg.ID, err
|
2018-02-22 23:49:32 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
return msg.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend nick if configured
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetBool("PrefixMessagesWithNick") {
|
2018-02-24 22:35:53 +00:00
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// Edit message if we have an ID
|
|
|
|
if msg.ID != "" {
|
|
|
|
ts := strings.Fields(msg.ID)
|
2018-07-13 21:23:11 +00:00
|
|
|
_, _, _, err := b.sc.UpdateMessage(channelID, ts[1], msg.Text)
|
2016-09-05 14:34:37 +00:00
|
|
|
if err != nil {
|
2018-02-24 22:35:53 +00:00
|
|
|
return msg.ID, err
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
return msg.ID, nil
|
2016-10-08 19:57:03 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
|
|
|
|
// create slack new post parameters
|
2016-11-05 00:11:28 +00:00
|
|
|
np := slack.NewPostMessageParameters()
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetBool("PrefixMessagesWithNick") {
|
2016-11-05 00:11:28 +00:00
|
|
|
np.AsUser = true
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
np.Username = msg.Username
|
|
|
|
np.LinkNames = 1 // replace mentions
|
2018-03-04 22:52:14 +00:00
|
|
|
np.IconURL = config.GetIconURL(&msg, b.GetString("iconurl"))
|
2016-11-05 23:46:32 +00:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
np.IconURL = msg.Avatar
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
// add a callback ID so we can see we created it
|
2018-05-27 19:50:00 +00:00
|
|
|
np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid})
|
2018-02-24 22:35:53 +00:00
|
|
|
// add file attachments
|
2017-09-18 21:51:27 +00:00
|
|
|
np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...)
|
2018-02-24 22:35:53 +00:00
|
|
|
// add slack attachments (from another slack bridge)
|
2018-02-22 23:49:32 +00:00
|
|
|
if len(msg.Extra["slack_attachment"]) > 0 {
|
|
|
|
for _, attach := range msg.Extra["slack_attachment"] {
|
|
|
|
np.Attachments = append(np.Attachments, attach.([]slack.Attachment)...)
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 21:51:27 +00:00
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// Upload a file if it exists
|
2017-11-03 22:10:16 +00:00
|
|
|
if msg.Extra != nil {
|
2018-02-03 00:11:11 +00:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-07-13 21:23:11 +00:00
|
|
|
b.sc.PostMessage(channelID, rmsg.Username+rmsg.Text, np)
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
2017-11-03 22:10:16 +00:00
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
2018-07-13 21:23:11 +00:00
|
|
|
b.handleUploadFile(&msg, channelID)
|
2017-11-03 22:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// Post normal message
|
2018-07-13 21:23:11 +00:00
|
|
|
_, id, err := b.sc.PostMessage(channelID, msg.Text, np)
|
2017-08-28 18:29:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "slack " + id, nil
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func (b *Bslack) Reload(cfg *bridge.Config) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 14:44:15 +00:00
|
|
|
func (b *Bslack) getAvatar(userid string) string {
|
2016-11-05 23:46:32 +00:00
|
|
|
var avatar string
|
|
|
|
if b.Users != nil {
|
|
|
|
for _, u := range b.Users {
|
2018-08-06 14:44:15 +00:00
|
|
|
if userid == u.ID {
|
2016-11-05 23:46:32 +00:00
|
|
|
return u.Profile.Image48
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return avatar
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:28:23 +00:00
|
|
|
/*
|
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-11-13 22:06:37 +00:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, 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-11-13 22:06:37 +00:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.Account, name)
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
2018-07-13 21:28:23 +00:00
|
|
|
*/
|
2016-09-05 14:34:37 +00:00
|
|
|
|
2017-02-03 15:40:15 +00: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)
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
func (b *Bslack) handleSlack() {
|
2018-02-24 22:35:53 +00:00
|
|
|
messages := make(chan *config.Message)
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookBindAddress") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Choosing webhooks based receiving")
|
2018-02-24 22:35:53 +00:00
|
|
|
go b.handleMatterHook(messages)
|
2017-06-24 17:36:10 +00:00
|
|
|
} else {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Choosing token based receiving")
|
2018-02-24 22:35:53 +00:00
|
|
|
go b.handleSlackClient(messages)
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debug("Start listening for Slack messages")
|
2018-02-24 22:35:53 +00:00
|
|
|
for message := range messages {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account)
|
2017-09-21 20:35:21 +00:00
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// cleanup the message
|
|
|
|
message.Text = b.replaceMention(message.Text)
|
|
|
|
message.Text = b.replaceVariable(message.Text)
|
|
|
|
message.Text = b.replaceChannel(message.Text)
|
2018-03-05 19:19:43 +00:00
|
|
|
message.Text = b.replaceURL(message.Text)
|
|
|
|
message.Text = html.UnescapeString(message.Text)
|
2018-02-22 23:49:32 +00:00
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// Add the avatar
|
2018-08-06 14:44:15 +00:00
|
|
|
message.Avatar = b.getAvatar(message.UserID)
|
2018-02-03 00:11:11 +00:00
|
|
|
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("<= Message is %#v", message)
|
2018-02-24 22:35:53 +00:00
|
|
|
b.Remote <- *message
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
func (b *Bslack) handleSlackClient(messages chan *config.Message) {
|
2016-09-05 14:34:37 +00:00
|
|
|
for msg := range b.rtm.IncomingEvents {
|
2018-01-02 13:39:27 +00:00
|
|
|
if msg.Type != "user_typing" && msg.Type != "latency_report" {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("== Receiving event %#v", msg.Data)
|
2018-01-02 13:39:27 +00:00
|
|
|
}
|
2016-09-05 14:34:37 +00:00
|
|
|
switch ev := msg.Data.(type) {
|
|
|
|
case *slack.MessageEvent:
|
2018-02-24 22:35:53 +00:00
|
|
|
if b.skipMessageEvent(ev) {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Skipped message: %#v", ev)
|
2018-02-03 00:15:57 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
rmsg, err := b.handleMessageEvent(ev)
|
2017-09-07 21:47:23 +00:00
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Errorf("%#v", err)
|
2017-09-07 21:47:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
messages <- rmsg
|
2016-09-05 14:34:37 +00:00
|
|
|
case *slack.OutgoingErrorEvent:
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("%#v", ev.Error())
|
2016-11-20 21:40:09 +00:00
|
|
|
case *slack.ChannelJoinedEvent:
|
|
|
|
b.Users, _ = b.sc.GetUsers()
|
2018-02-28 21:54:47 +00:00
|
|
|
b.Usergroups, _ = b.sc.GetUserGroups()
|
2016-09-05 14:34:37 +00:00
|
|
|
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()
|
2018-02-28 21:54:47 +00:00
|
|
|
b.Usergroups, _ = b.sc.GetUserGroups()
|
2017-01-27 23:36:53 +00:00
|
|
|
// 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 14:34:37 +00:00
|
|
|
case *slack.InvalidAuthEvent:
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Fatalf("Invalid Token %#v", ev)
|
2018-01-03 13:06:28 +00:00
|
|
|
case *slack.ConnectionErrorEvent:
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Errorf("Connection failed %#v %#v", ev.Error(), ev.ErrorObj)
|
2016-09-05 14:34:37 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
func (b *Bslack) handleMatterHook(messages chan *config.Message) {
|
2016-09-05 14:34:37 +00:00
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("receiving from matterhook (slack) %#v", message)
|
2018-02-24 22:35:53 +00:00
|
|
|
if message.UserName == "slackbot" {
|
2017-02-17 22:13:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
messages <- &config.Message{Username: message.UserName, Text: message.Text, Channel: message.ChannelName}
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 21:40:09 +00:00
|
|
|
|
|
|
|
func (b *Bslack) userName(id string) string {
|
|
|
|
for _, u := range b.Users {
|
|
|
|
if u.ID == id {
|
2017-11-02 16:11:42 +00:00
|
|
|
if u.Profile.DisplayName != "" {
|
|
|
|
return u.Profile.DisplayName
|
|
|
|
}
|
2016-11-20 21:40:09 +00:00
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:08:39 +00:00
|
|
|
/*
|
2018-02-28 21:54:47 +00:00
|
|
|
func (b *Bslack) userGroupName(id string) string {
|
|
|
|
for _, u := range b.Usergroups {
|
|
|
|
if u.ID == id {
|
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-03-03 10:08:39 +00:00
|
|
|
*/
|
2018-02-28 21:54:47 +00:00
|
|
|
|
2017-11-03 19:32:28 +00:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
2016-11-20 21:40:09 +00:00
|
|
|
func (b *Bslack) replaceMention(text string) string {
|
2018-05-29 20:49:10 +00:00
|
|
|
results := regexp.MustCompile(`<@([a-zA-Z0-9]+)>`).FindAllStringSubmatch(text, -1)
|
2016-11-20 21:40:09 +00:00
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, "<@"+r[1]+">", "@"+b.userName(r[1]), -1)
|
2017-11-02 20:21:46 +00:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2016-11-20 21:40:09 +00:00
|
|
|
|
2017-11-03 19:32:28 +00:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
2017-11-02 20:21:46 +00:00
|
|
|
func (b *Bslack) replaceChannel(text string) string {
|
|
|
|
results := regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`).FindAllStringSubmatch(text, -1)
|
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, r[0], "#"+r[1], -1)
|
2016-11-20 21:40:09 +00:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2017-06-26 20:16:19 +00:00
|
|
|
|
2017-11-03 19:32:28 +00:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#variables
|
|
|
|
func (b *Bslack) replaceVariable(text string) string {
|
2018-03-05 19:56:33 +00:00
|
|
|
results := regexp.MustCompile(`<!((?:subteam\^)?[a-zA-Z0-9]+)(?:\|@?(.+?))?>`).FindAllStringSubmatch(text, -1)
|
2017-11-03 19:32:28 +00:00
|
|
|
for _, r := range results {
|
2018-03-02 21:30:44 +00:00
|
|
|
if r[2] != "" {
|
|
|
|
text = strings.Replace(text, r[0], "@"+r[2], -1)
|
|
|
|
} else {
|
|
|
|
text = strings.Replace(text, r[0], "@"+r[1], -1)
|
|
|
|
}
|
2017-11-03 19:32:28 +00:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_urls
|
2017-06-26 20:16:19 +00:00
|
|
|
func (b *Bslack) replaceURL(text string) string {
|
2017-10-26 19:58:43 +00:00
|
|
|
results := regexp.MustCompile(`<(.*?)(\|.*?)?>`).FindAllStringSubmatch(text, -1)
|
2017-06-26 20:16:19 +00:00
|
|
|
for _, r := range results {
|
2018-03-22 21:28:27 +00:00
|
|
|
if len(strings.TrimSpace(r[2])) == 1 { // A display text separator was found, but the text was blank
|
|
|
|
text = strings.Replace(text, r[0], "", -1)
|
|
|
|
} else {
|
|
|
|
text = strings.Replace(text, r[0], r[1], -1)
|
|
|
|
}
|
2017-06-26 20:16:19 +00:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2017-09-18 21:51:27 +00:00
|
|
|
|
2017-09-21 20:35:21 +00:00
|
|
|
func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment {
|
2017-09-18 21:51:27 +00:00
|
|
|
var attachs []slack.Attachment
|
2017-09-21 20:35:21 +00:00
|
|
|
for _, v := range extra["attachments"] {
|
2017-09-18 22:04:27 +00:00
|
|
|
entry := v.(map[string]interface{})
|
|
|
|
s := slack.Attachment{}
|
|
|
|
s.Fallback = entry["fallback"].(string)
|
|
|
|
s.Color = entry["color"].(string)
|
|
|
|
s.Pretext = entry["pretext"].(string)
|
|
|
|
s.AuthorName = entry["author_name"].(string)
|
|
|
|
s.AuthorLink = entry["author_link"].(string)
|
|
|
|
s.AuthorIcon = entry["author_icon"].(string)
|
|
|
|
s.Title = entry["title"].(string)
|
|
|
|
s.TitleLink = entry["title_link"].(string)
|
|
|
|
s.Text = entry["text"].(string)
|
|
|
|
s.ImageURL = entry["image_url"].(string)
|
|
|
|
s.ThumbURL = entry["thumb_url"].(string)
|
|
|
|
s.Footer = entry["footer"].(string)
|
|
|
|
s.FooterIcon = entry["footer_icon"].(string)
|
|
|
|
attachs = append(attachs, s)
|
2017-09-18 21:51:27 +00:00
|
|
|
}
|
|
|
|
return attachs
|
|
|
|
}
|
2017-09-21 20:35:21 +00:00
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// handleDownloadFile handles file download
|
|
|
|
func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) error {
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
|
|
|
// limit to 1MB for now
|
|
|
|
comment := ""
|
|
|
|
results := regexp.MustCompile(`.*?commented: (.*)`).FindAllStringSubmatch(rmsg.Text, -1)
|
|
|
|
if len(results) > 0 {
|
|
|
|
comment = results[0][1]
|
2017-09-21 20:35:21 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
err := helper.HandleDownloadSize(b.Log, rmsg, file.Name, int64(file.Size), b.General)
|
2017-09-21 20:35:21 +00:00
|
|
|
if err != nil {
|
2018-02-24 22:35:53 +00:00
|
|
|
return err
|
2017-09-21 20:35:21 +00:00
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
// actually download the file
|
2018-03-04 22:52:14 +00:00
|
|
|
data, err := helper.DownloadFileAuth(file.URLPrivateDownload, "Bearer "+b.GetString("Token"))
|
2018-02-24 22:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)
|
|
|
|
}
|
|
|
|
// add the downloaded data to the message
|
2018-02-26 23:33:21 +00:00
|
|
|
helper.HandleDownloadData(b.Log, rmsg, file.Name, comment, file.URLPrivateDownload, data, b.General)
|
2018-02-24 22:35:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUploadFile handles native upload of files
|
|
|
|
func (b *Bslack) handleUploadFile(msg *config.Message, channelID string) (string, error) {
|
|
|
|
var err error
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
_, err = b.sc.UploadFile(slack.FileUploadParameters{
|
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
Filename: fi.Name,
|
|
|
|
Channels: []string{channelID},
|
|
|
|
InitialComment: fi.Comment,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Errorf("uploadfile %#v", err)
|
2018-02-24 22:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleMessageEvent handles the message events
|
|
|
|
func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, error) {
|
|
|
|
// update the userlist on a channel_join
|
|
|
|
if ev.SubType == "channel_join" {
|
|
|
|
b.Users, _ = b.sc.GetUsers()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit message
|
2018-03-04 22:52:14 +00:00
|
|
|
if !b.GetBool("EditDisable") && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("SubMessage %#v", ev.SubMessage)
|
2018-02-24 22:35:53 +00:00
|
|
|
ev.User = ev.SubMessage.User
|
2018-03-04 22:52:14 +00:00
|
|
|
ev.Text = ev.SubMessage.Text + b.GetString("EditSuffix")
|
2018-02-24 22:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use our own func because rtm.GetChannelInfo doesn't work for private channels
|
|
|
|
channel, err := b.getChannelByID(ev.Channel)
|
2017-09-21 20:35:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
|
|
|
|
rmsg := config.Message{Text: ev.Text, Channel: channel.Name, Account: b.Account, ID: "slack " + ev.Timestamp, Extra: make(map[string][]interface{})}
|
|
|
|
|
2018-07-13 21:23:11 +00:00
|
|
|
if b.UseChannelID {
|
|
|
|
rmsg.Channel = "ID:" + channel.ID
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// find the user id and name
|
2018-04-25 19:27:34 +00:00
|
|
|
if ev.User != "" && ev.SubType != messageDeleted && ev.SubType != "file_comment" {
|
2018-02-24 22:35:53 +00:00
|
|
|
user, err := b.rtm.GetUserInfo(ev.User)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rmsg.UserID = user.ID
|
|
|
|
rmsg.Username = user.Name
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
rmsg.Username = user.Profile.DisplayName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we have some text in the attachments
|
|
|
|
if rmsg.Text == "" {
|
|
|
|
for _, attach := range ev.Attachments {
|
|
|
|
if attach.Text != "" {
|
2018-06-13 19:58:51 +00:00
|
|
|
if attach.Title != "" {
|
|
|
|
rmsg.Text = attach.Title + "\n"
|
|
|
|
}
|
|
|
|
rmsg.Text += attach.Text
|
2018-02-24 22:35:53 +00:00
|
|
|
} else {
|
|
|
|
rmsg.Text = attach.Fallback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// when using webhookURL we can't check if it's our webhook or not for now
|
2018-04-25 19:27:34 +00:00
|
|
|
if rmsg.Username == "" && ev.BotID != "" && b.GetString("WebhookURL") == "" {
|
2018-02-24 22:35:53 +00:00
|
|
|
bot, err := b.rtm.GetBotInfo(ev.BotID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if bot.Name != "" {
|
|
|
|
rmsg.Username = bot.Name
|
|
|
|
if ev.Username != "" {
|
|
|
|
rmsg.Username = ev.Username
|
|
|
|
}
|
|
|
|
rmsg.UserID = bot.ID
|
|
|
|
}
|
2018-04-19 23:01:45 +00:00
|
|
|
|
|
|
|
// fixes issues with matterircd users
|
|
|
|
if bot.Name == "Slack API Tester" {
|
|
|
|
user, err := b.rtm.GetUserInfo(ev.User)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rmsg.UserID = user.ID
|
|
|
|
rmsg.Username = user.Name
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
rmsg.Username = user.Profile.DisplayName
|
|
|
|
}
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// file comments are set by the system (because there is no username given)
|
|
|
|
if ev.SubType == "file_comment" {
|
|
|
|
rmsg.Username = "system"
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we have a /me action
|
|
|
|
if ev.SubType == "me_message" {
|
|
|
|
rmsg.Event = config.EVENT_USER_ACTION
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle join/leave
|
|
|
|
if ev.SubType == "channel_leave" || ev.SubType == "channel_join" {
|
|
|
|
rmsg.Username = "system"
|
|
|
|
rmsg.Event = config.EVENT_JOIN_LEAVE
|
|
|
|
}
|
|
|
|
|
|
|
|
// edited messages have a submessage, use this timestamp
|
|
|
|
if ev.SubMessage != nil {
|
|
|
|
rmsg.ID = "slack " + ev.SubMessage.Timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleted message event
|
|
|
|
if ev.SubType == messageDeleted {
|
|
|
|
rmsg.Text = config.EVENT_MSG_DELETE
|
|
|
|
rmsg.Event = config.EVENT_MSG_DELETE
|
|
|
|
rmsg.ID = "slack " + ev.DeletedTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// topic change event
|
|
|
|
if ev.SubType == "channel_topic" || ev.SubType == "channel_purpose" {
|
|
|
|
rmsg.Event = config.EVENT_TOPIC_CHANGE
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only deleted messages can have a empty username and text
|
|
|
|
if (rmsg.Text == "" || rmsg.Username == "") && ev.SubType != messageDeleted {
|
2018-05-27 20:14:31 +00:00
|
|
|
// this is probably a webhook we couldn't resolve
|
|
|
|
if ev.BotID != "" {
|
|
|
|
return nil, fmt.Errorf("probably an incoming webhook we couldn't resolve (maybe ourselves)")
|
|
|
|
}
|
2018-02-24 22:35:53 +00:00
|
|
|
return nil, fmt.Errorf("empty message and not a deleted message")
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the attachments, so that we can send them to other slack (compatible) bridges
|
|
|
|
if len(ev.Attachments) > 0 {
|
|
|
|
rmsg.Extra["slack_attachment"] = append(rmsg.Extra["slack_attachment"], ev.Attachments)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
|
|
|
if ev.File != nil {
|
|
|
|
err := b.handleDownloadFile(&rmsg, ev.File)
|
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Errorf("download failed: %s", err)
|
2018-02-24 22:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rmsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-04 22:52:14 +00:00
|
|
|
if b.GetBool("PrefixMessagesWithNick") {
|
2018-02-24 22:35:53 +00: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-05-27 20:30:17 +00:00
|
|
|
iconURL := config.GetIconURL(&rmsg, b.GetString("iconurl"))
|
|
|
|
matterMessage := matterhook.OMessage{IconURL: iconURL, Channel: msg.Channel, UserName: rmsg.Username, Text: rmsg.Text}
|
2018-02-24 22:35:53 +00:00
|
|
|
b.mh.Send(matterMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// webhook doesn't support file uploads, so we add the url manually
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text += " " + fi.URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have native slack_attachments add them
|
|
|
|
var attachs []slack.Attachment
|
|
|
|
if len(msg.Extra["slack_attachment"]) > 0 {
|
|
|
|
for _, attach := range msg.Extra["slack_attachment"] {
|
|
|
|
attachs = append(attachs, attach.([]slack.Attachment)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:30:17 +00:00
|
|
|
iconURL := config.GetIconURL(&msg, b.GetString("iconurl"))
|
|
|
|
matterMessage := matterhook.OMessage{IconURL: iconURL, Attachments: attachs, Channel: msg.Channel, UserName: msg.Username, Text: msg.Text}
|
2018-02-24 22:35:53 +00:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
matterMessage.IconURL = msg.Avatar
|
|
|
|
}
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Error(err)
|
2018-02-24 22:35:53 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// skipMessageEvent skips event that need to be skipped :-)
|
|
|
|
func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
2018-03-06 20:34:55 +00:00
|
|
|
if ev.SubType == "channel_leave" || ev.SubType == "channel_join" {
|
|
|
|
return b.GetBool("nosendjoinpart")
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:35:53 +00:00
|
|
|
// ignore pinned items
|
|
|
|
if ev.SubType == "pinned_item" || ev.SubType == "unpinned_item" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not send messages from ourself
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" && ev.Username == b.si.User.Name {
|
2018-02-24 22:35:53 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip messages we made ourselves
|
|
|
|
if len(ev.Attachments) > 0 {
|
2018-05-27 19:50:00 +00:00
|
|
|
if ev.Attachments[0].CallbackID == "matterbridge_"+b.uuid {
|
2018-02-24 22:35:53 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
if !b.GetBool("EditDisable") && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
2018-02-24 22:35:53 +00:00
|
|
|
// it seems ev.SubMessage.Edited == nil when slack unfurls
|
|
|
|
// do not forward these messages #266
|
|
|
|
if ev.SubMessage.Edited == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-09-21 20:35:21 +00:00
|
|
|
}
|
2018-07-13 21:23:11 +00:00
|
|
|
|
|
|
|
func (b *Bslack) getChannelID(name string) string {
|
|
|
|
idcheck := strings.Split(name, "ID:")
|
|
|
|
if len(idcheck) > 1 {
|
|
|
|
return idcheck[1]
|
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.Name == name {
|
|
|
|
return channel.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|