2016-08-14 12:48:51 -07:00
|
|
|
package bmattermost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/matterclient"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MMhook struct {
|
|
|
|
mh *matterhook.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type MMapi struct {
|
|
|
|
mc *matterclient.MMClient
|
|
|
|
mmMap map[string]string
|
|
|
|
mmIgnoreNicks []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MMMessage struct {
|
|
|
|
Text string
|
|
|
|
Channel string
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bmattermost struct {
|
|
|
|
MMhook
|
|
|
|
MMapi
|
2016-11-13 14:06:37 -08:00
|
|
|
Config *config.Protocol
|
|
|
|
Remote chan config.Message
|
|
|
|
name string
|
|
|
|
TeamId string
|
|
|
|
Account string
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:58:57 -07:00
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "mattermost"
|
2016-08-14 12:48:51 -07:00
|
|
|
|
|
|
|
func init() {
|
2016-09-19 14:58:57 -07:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-11-13 14:06:37 -08:00
|
|
|
func New(cfg config.Protocol, account string, c chan config.Message) *Bmattermost {
|
2016-08-14 12:48:51 -07:00
|
|
|
b := &Bmattermost{}
|
2016-09-18 10:21:15 -07:00
|
|
|
b.Config = &cfg
|
2016-08-14 12:48:51 -07:00
|
|
|
b.Remote = c
|
2016-11-13 14:06:37 -08:00
|
|
|
b.Account = account
|
2016-08-14 12:48:51 -07:00
|
|
|
b.mmMap = make(map[string]string)
|
2016-08-15 14:16:07 -07:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) Command(cmd string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) Connect() error {
|
2016-09-19 14:58:57 -07:00
|
|
|
if !b.Config.UseAPI {
|
|
|
|
flog.Info("Connecting webhooks")
|
2016-09-18 10:21:15 -07:00
|
|
|
b.mh = matterhook.New(b.Config.URL,
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
BindAddress: b.Config.BindAddress})
|
2016-08-14 12:48:51 -07:00
|
|
|
} else {
|
2016-09-18 10:21:15 -07:00
|
|
|
b.mc = matterclient.New(b.Config.Login, b.Config.Password,
|
|
|
|
b.Config.Team, b.Config.Server)
|
|
|
|
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
|
|
|
|
b.mc.NoTLS = b.Config.NoTLS
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Infof("Connecting %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server)
|
2016-08-14 12:48:51 -07:00
|
|
|
err := b.mc.Login()
|
|
|
|
if err != nil {
|
2016-08-15 14:16:07 -07:00
|
|
|
return err
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Info("Connection succeeded")
|
2016-10-29 07:46:02 -07:00
|
|
|
b.TeamId = b.mc.GetTeamId()
|
2016-08-14 12:48:51 -07:00
|
|
|
go b.mc.WsReceiver()
|
|
|
|
}
|
|
|
|
go b.handleMatter()
|
2016-08-15 14:16:07 -07:00
|
|
|
return nil
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
func (b *Bmattermost) JoinChannel(channel string) error {
|
2016-09-20 03:20:44 -07:00
|
|
|
// we can only join channels using the API
|
|
|
|
if b.Config.UseAPI {
|
2016-09-30 13:59:30 -07:00
|
|
|
return b.mc.JoinChannel(b.mc.GetChannelId(channel, ""))
|
2016-09-20 03:20:44 -07:00
|
|
|
}
|
|
|
|
return nil
|
2016-09-18 10:21:15 -07:00
|
|
|
}
|
|
|
|
|
2016-08-14 12:48:51 -07:00
|
|
|
func (b *Bmattermost) Send(msg config.Message) error {
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2016-11-13 14:06:37 -08:00
|
|
|
nick := msg.Username
|
2016-11-04 17:11:28 -07:00
|
|
|
message := msg.Text
|
|
|
|
channel := msg.Channel
|
2016-08-14 12:48:51 -07:00
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
if b.Config.PrefixMessagesWithNick {
|
2016-08-14 12:48:51 -07:00
|
|
|
/*if IsMarkup(message) {
|
|
|
|
message = nick + "\n\n" + message
|
|
|
|
} else {
|
|
|
|
*/
|
|
|
|
message = nick + " " + message
|
|
|
|
//}
|
|
|
|
}
|
2016-09-19 14:58:57 -07:00
|
|
|
if !b.Config.UseAPI {
|
2016-09-18 10:21:15 -07:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
2016-08-14 12:48:51 -07:00
|
|
|
matterMessage.Channel = channel
|
|
|
|
matterMessage.UserName = nick
|
2016-11-04 17:11:28 -07:00
|
|
|
matterMessage.Type = ""
|
2016-08-14 12:48:51 -07:00
|
|
|
matterMessage.Text = message
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Info(err)
|
2016-08-14 12:48:51 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b.mc.PostMessage(b.mc.GetChannelId(channel, ""), message)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) handleMatter() {
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Debugf("Choosing API based Mattermost connection: %t", b.Config.UseAPI)
|
2016-08-14 12:48:51 -07:00
|
|
|
mchan := make(chan *MMMessage)
|
2016-09-19 14:58:57 -07:00
|
|
|
if b.Config.UseAPI {
|
2016-08-14 12:48:51 -07:00
|
|
|
go b.handleMatterClient(mchan)
|
|
|
|
} else {
|
|
|
|
go b.handleMatterHook(mchan)
|
|
|
|
}
|
|
|
|
for message := range mchan {
|
2016-11-13 14:06:37 -08:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account)
|
|
|
|
b.Remote <- config.Message{Text: message.Text, Username: message.Username, Channel: message.Channel, Account: b.Account}
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
|
|
|
|
for message := range b.mc.MessageChan {
|
|
|
|
// do not post our own messages back to irc
|
2016-10-23 12:16:14 -07:00
|
|
|
// only listen to message from our team
|
2016-11-12 13:00:53 -08:00
|
|
|
if message.Raw.Event == "posted" && b.mc.User.Username != message.Username && message.Raw.Data["team_id"].(string) == b.TeamId {
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Debugf("Receiving from matterclient %#v", message)
|
2016-08-14 12:48:51 -07:00
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = message.Username
|
|
|
|
m.Channel = message.Channel
|
|
|
|
m.Text = message.Text
|
2016-11-26 06:46:39 -08:00
|
|
|
if len(message.Post.FileIds) > 0 {
|
|
|
|
for _, link := range b.mc.GetPublicLinks(message.Post.FileIds) {
|
2016-09-20 14:48:58 -07:00
|
|
|
m.Text = m.Text + "\n" + link
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 12:48:51 -07:00
|
|
|
mchan <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) handleMatterHook(mchan chan *MMMessage) {
|
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
2016-09-19 14:58:57 -07:00
|
|
|
flog.Debugf("Receiving from matterhook %#v", message)
|
2016-08-14 12:48:51 -07:00
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = message.UserName
|
|
|
|
m.Text = message.Text
|
|
|
|
m.Channel = message.ChannelName
|
|
|
|
mchan <- m
|
|
|
|
}
|
|
|
|
}
|