summaryrefslogtreecommitdiffstats
path: root/bridge/rocketchat/rocketchat.go
blob: d87450ec19724ce7f37461375f99ced31cfa6d99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
	name    string
	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")
	b.mh = matterhook.New(b.Config.URL,
		matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
			DisableServer: true})
	b.rh = rockethook.New(b.Config.URL, rockethook.Config{BindAddress: b.Config.BindAddress})
	go b.handleRocketHook()
	return nil
}

func (b *Brocketchat) JoinChannel(channel string) error {
	return nil
}

func (b *Brocketchat) Send(msg config.Message) error {
	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)
		return err
	}
	return nil
}

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)
		b.Remote <- config.Message{Text: message.Text, Username: message.UserName, Channel: message.ChannelName, Account: b.Account}
	}
}