2016-12-03 00:10:29 +01:00
|
|
|
package brocketchat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/hook/rockethook"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MMhook struct {
|
|
|
|
mh *matterhook.Client
|
|
|
|
rh *rockethook.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type Brocketchat struct {
|
|
|
|
MMhook
|
|
|
|
Config *config.Protocol
|
|
|
|
Remote chan config.Message
|
|
|
|
Account string
|
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "rocketchat"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg config.Protocol, account string, c chan config.Message) *Brocketchat {
|
|
|
|
b := &Brocketchat{}
|
|
|
|
b.Config = &cfg
|
|
|
|
b.Remote = c
|
|
|
|
b.Account = account
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Command(cmd string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Connect() error {
|
|
|
|
flog.Info("Connecting webhooks")
|
2017-06-24 19:36:10 +02:00
|
|
|
b.mh = matterhook.New(b.Config.WebhookURL,
|
2016-12-03 00:10:29 +01:00
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
DisableServer: true})
|
2017-06-24 19:36:10 +02:00
|
|
|
b.rh = rockethook.New(b.Config.WebhookURL, rockethook.Config{BindAddress: b.Config.WebhookBindAddress})
|
2016-12-03 00:10:29 +01:00
|
|
|
go b.handleRocketHook()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:12:02 +01:00
|
|
|
func (b *Brocketchat) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:51:41 +02:00
|
|
|
func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
|
2016-12-03 00:10:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:59:37 +02:00
|
|
|
func (b *Brocketchat) Send(msg config.Message) (string, error) {
|
2017-09-11 22:45:15 +02:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-12-03 00:10:29 +01:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
|
|
|
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 {
|
|
|
|
flog.Info(err)
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", err
|
2016-12-03 00:10:29 +01:00
|
|
|
}
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", nil
|
2016-12-03 00:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) handleRocketHook() {
|
|
|
|
for {
|
|
|
|
message := b.rh.Receive()
|
|
|
|
flog.Debugf("Receiving from rockethook %#v", message)
|
|
|
|
// do not loop
|
|
|
|
if message.UserName == b.Config.Nick {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", message.UserName, b.Account)
|
2017-06-18 15:44:54 +02:00
|
|
|
b.Remote <- config.Message{Text: message.Text, Username: message.UserName, Channel: message.ChannelName, Account: b.Account, UserID: message.UserID}
|
2016-12-03 00:10:29 +01:00
|
|
|
}
|
|
|
|
}
|