2016-12-02 23:10:29 +00:00
|
|
|
package brocketchat
|
|
|
|
|
|
|
|
import (
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-12-02 23:10:29 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-03 00:11:11 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-12-02 23:10:29 +00: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
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
return &Brocketchat{Config: cfg}
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Command(cmd string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Connect() error {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting webhooks")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
2016-12-02 23:10:29 +00:00
|
|
|
DisableServer: true})
|
2018-03-04 22:52:14 +00:00
|
|
|
b.rh = rockethook.New(b.GetString("WebhookURL"), rockethook.Config{BindAddress: b.GetString("WebhookBindAddress")})
|
2016-12-02 23:10:29 +00:00
|
|
|
go b.handleRocketHook()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Brocketchat) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
|
2016-12-02 23:10:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Brocketchat) Send(msg config.Message) (string, error) {
|
2017-09-11 20:45:15 +00:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
return "", nil
|
|
|
|
}
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-03 00:11:11 +00:00
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-03-05 19:19:43 +00:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Channel: rmsg.Channel, UserName: rmsg.Username, Text: rmsg.Text}
|
2018-02-03 00:11:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL")}
|
2016-12-02 23:10:29 +00:00
|
|
|
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 23:33:21 +00:00
|
|
|
b.Log.Info(err)
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", err
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", nil
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) handleRocketHook() {
|
|
|
|
for {
|
|
|
|
message := b.rh.Receive()
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Receiving from rockethook %#v", message)
|
2016-12-02 23:10:29 +00:00
|
|
|
// do not loop
|
2018-03-04 22:52:14 +00:00
|
|
|
if message.UserName == b.GetString("Nick") {
|
2016-12-02 23:10:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.UserName, b.Account)
|
2017-06-18 13:44:54 +00:00
|
|
|
b.Remote <- config.Message{Text: message.Text, Username: message.UserName, Channel: message.ChannelName, Account: b.Account, UserID: message.UserID}
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
}
|