summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-02-20 00:49:27 +0100
committerWim <wim@42.be>2017-02-20 00:50:37 +0100
commitc5cfbc22972c397805c8385840d6cfd0de307c87 (patch)
tree336b2676a878092cb421ca17590db40c5da86a23 /bridge
parentcd0a2beb11efb4f03fa9fab38b93391b346864e9 (diff)
downloadmatterbridge-msglm-c5cfbc22972c397805c8385840d6cfd0de307c87.tar.gz
matterbridge-msglm-c5cfbc22972c397805c8385840d6cfd0de307c87.tar.bz2
matterbridge-msglm-c5cfbc22972c397805c8385840d6cfd0de307c87.zip
Add matrix support
Diffstat (limited to 'bridge')
-rw-r--r--bridge/bridge.go4
-rw-r--r--bridge/config/config.go1
-rw-r--r--bridge/matrix/matrix.go92
3 files changed, 97 insertions, 0 deletions
diff --git a/bridge/bridge.go b/bridge/bridge.go
index 012b0efd..312792b6 100644
--- a/bridge/bridge.go
+++ b/bridge/bridge.go
@@ -6,6 +6,7 @@ import (
"github.com/42wim/matterbridge/bridge/discord"
"github.com/42wim/matterbridge/bridge/gitter"
"github.com/42wim/matterbridge/bridge/irc"
+ "github.com/42wim/matterbridge/bridge/matrix"
"github.com/42wim/matterbridge/bridge/mattermost"
"github.com/42wim/matterbridge/bridge/rocketchat"
"github.com/42wim/matterbridge/bridge/slack"
@@ -71,6 +72,9 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid
case "rocketchat":
b.Config = cfg.Rocketchat[name]
b.Bridger = brocketchat.New(cfg.Rocketchat[name], bridge.Account, c)
+ case "matrix":
+ b.Config = cfg.Matrix[name]
+ b.Bridger = bmatrix.New(cfg.Matrix[name], bridge.Account, c)
case "api":
b.Config = cfg.Api[name]
b.Bridger = api.New(cfg.Api[name], bridge.Account, c)
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 4f6568ab..5077abb5 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -86,6 +86,7 @@ type Config struct {
Api map[string]Protocol
IRC map[string]Protocol
Mattermost map[string]Protocol
+ Matrix map[string]Protocol
Slack map[string]Protocol
Gitter map[string]Protocol
Xmpp map[string]Protocol
diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go
new file mode 100644
index 00000000..222a8fd0
--- /dev/null
+++ b/bridge/matrix/matrix.go
@@ -0,0 +1,92 @@
+package bmatrix
+
+import (
+ "github.com/42wim/matterbridge/bridge/config"
+ log "github.com/Sirupsen/logrus"
+ matrix "github.com/matrix-org/gomatrix"
+)
+
+type Bmatrix struct {
+ mc *matrix.Client
+ Config *config.Protocol
+ Remote chan config.Message
+ Account string
+ UserID string
+}
+
+var flog *log.Entry
+var protocol = "matrix"
+
+func init() {
+ flog = log.WithFields(log.Fields{"module": protocol})
+}
+
+func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix {
+ b := &Bmatrix{}
+ b.Config = &cfg
+ b.Account = account
+ b.Remote = c
+ return b
+}
+
+func (b *Bmatrix) Connect() error {
+ var err error
+ flog.Infof("Connecting %s", b.Config.Server)
+ b.mc, err = matrix.NewClient(b.Config.Server, "", "")
+ if err != nil {
+ flog.Debugf("%#v", err)
+ return err
+ }
+ resp, err := b.mc.Login(&matrix.ReqLogin{
+ Type: "m.login.password",
+ User: b.Config.Login,
+ Password: b.Config.Password,
+ })
+ if err != nil {
+ flog.Debugf("%#v", err)
+ return err
+ }
+ b.mc.SetCredentials(resp.UserID, resp.AccessToken)
+ b.UserID = resp.UserID
+ flog.Info("Connection succeeded")
+ go b.handlematrix()
+ return nil
+}
+
+func (b *Bmatrix) Disconnect() error {
+ return nil
+}
+
+func (b *Bmatrix) JoinChannel(channel string) error {
+ _, err := b.mc.JoinRoom(channel, "", nil)
+ return err
+}
+
+func (b *Bmatrix) Send(msg config.Message) error {
+ flog.Debugf("Receiving %#v", msg)
+ b.mc.SendText(msg.Channel, msg.Username+msg.Text)
+ return nil
+}
+
+func (b *Bmatrix) handlematrix() error {
+ warning := "Not relaying this message, please setup a dedicated bot user"
+ syncer := b.mc.Syncer.(*matrix.DefaultSyncer)
+ syncer.OnEventType("m.room.message", func(ev *matrix.Event) {
+ if ev.Content["msgtype"].(string) == "m.text" && ev.Sender != b.UserID {
+ flog.Debugf("Sending message from %s on %s to gateway", ev.Sender, b.Account)
+ b.Remote <- config.Message{Username: ev.Sender, Text: ev.Content["body"].(string), Channel: ev.RoomID, Account: b.Account}
+ }
+ if ev.Sender == b.UserID && ev.Content["body"].(string) != warning {
+ b.mc.SendText(ev.RoomID, warning)
+ }
+ flog.Debugf("Received: %#v", ev)
+ })
+ go func() {
+ for {
+ if err := b.mc.Sync(); err != nil {
+ flog.Println("Sync() returned ", err)
+ }
+ }
+ }()
+ return nil
+}