matterbridge/bridge/rocketchat/rocketchat.go

95 lines
2.4 KiB
Go
Raw Normal View History

2016-12-02 15:10:29 -08:00
package brocketchat
import (
2018-02-26 15:33:21 -08:00
"github.com/42wim/matterbridge/bridge"
2016-12-02 15:10:29 -08:00
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
2016-12-02 15:10:29 -08:00
"github.com/42wim/matterbridge/hook/rockethook"
"github.com/42wim/matterbridge/matterhook"
)
type MMhook struct {
mh *matterhook.Client
rh *rockethook.Client
}
type Brocketchat struct {
MMhook
*config.BridgeConfig
2016-12-02 15:10:29 -08:00
}
2018-02-26 15:33:21 -08:00
func New(cfg *config.BridgeConfig) bridge.Bridger {
return &Brocketchat{BridgeConfig: cfg}
2016-12-02 15:10:29 -08:00
}
func (b *Brocketchat) Command(cmd string) string {
return ""
}
func (b *Brocketchat) Connect() error {
2018-02-26 15:33:21 -08:00
b.Log.Info("Connecting webhooks")
b.mh = matterhook.New(b.Config.WebhookURL,
2016-12-02 15:10:29 -08:00
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
DisableServer: true})
b.rh = rockethook.New(b.Config.WebhookURL, rockethook.Config{BindAddress: b.Config.WebhookBindAddress})
2016-12-02 15:10:29 -08:00
go b.handleRocketHook()
return nil
}
func (b *Brocketchat) Disconnect() error {
return nil
}
func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
2016-12-02 15:10:29 -08:00
return nil
}
func (b *Brocketchat) Send(msg config.Message) (string, error) {
// ignore delete messages
if msg.Event == config.EVENT_MSG_DELETE {
return "", nil
}
2018-02-28 13:23:29 -08:00
b.Log.Debugf("=> Receiving %#v", msg)
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: rmsg.Channel, UserName: rmsg.Username,
Text: rmsg.Text}
b.mh.Send(matterMessage)
}
if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
if fi.URL != "" {
msg.Text += fi.URL
}
}
}
}
2016-12-02 15:10:29 -08:00
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.Channel = msg.Channel
matterMessage.UserName = msg.Username
matterMessage.Type = ""
matterMessage.Text = msg.Text
err := b.mh.Send(matterMessage)
if err != nil {
2018-02-26 15:33:21 -08:00
b.Log.Info(err)
return "", err
2016-12-02 15:10:29 -08:00
}
return "", nil
2016-12-02 15:10:29 -08:00
}
func (b *Brocketchat) handleRocketHook() {
for {
message := b.rh.Receive()
2018-02-26 15:33:21 -08:00
b.Log.Debugf("Receiving from rockethook %#v", message)
2016-12-02 15:10:29 -08:00
// do not loop
if message.UserName == b.Config.Nick {
continue
}
2018-02-28 13:23:29 -08:00
b.Log.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.ChannelName, Account: b.Account, UserID: message.UserID}
2016-12-02 15:10:29 -08:00
}
}