Use roomalias instead of internal ID (matrix)

This commit is contained in:
Wim 2017-02-24 17:58:51 +01:00
parent 8b286fb009
commit 84c0b745af
2 changed files with 33 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
matrix "github.com/matrix-org/gomatrix" matrix "github.com/matrix-org/gomatrix"
"sync"
) )
type Bmatrix struct { type Bmatrix struct {
@ -12,6 +13,8 @@ type Bmatrix struct {
Remote chan config.Message Remote chan config.Message
Account string Account string
UserID string UserID string
RoomMap map[string]string
sync.RWMutex
} }
var flog *log.Entry var flog *log.Entry
@ -23,6 +26,7 @@ func init() {
func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix { func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix {
b := &Bmatrix{} b := &Bmatrix{}
b.RoomMap = make(map[string]string)
b.Config = &cfg b.Config = &cfg
b.Account = account b.Account = account
b.Remote = c b.Remote = c
@ -58,26 +62,47 @@ func (b *Bmatrix) Disconnect() error {
} }
func (b *Bmatrix) JoinChannel(channel string) error { func (b *Bmatrix) JoinChannel(channel string) error {
_, err := b.mc.JoinRoom(channel, "", nil) resp, err := b.mc.JoinRoom(channel, "", nil)
if err != nil {
return err
}
b.Lock()
b.RoomMap[resp.RoomID] = channel
b.Unlock()
return err return err
} }
func (b *Bmatrix) Send(msg config.Message) error { func (b *Bmatrix) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
b.mc.SendText(msg.Channel, msg.Username+msg.Text) channel := b.getRoomID(msg.Channel)
flog.Debugf("Sending to channel %s", channel)
b.mc.SendText(channel, msg.Username+msg.Text)
return nil return nil
} }
func (b *Bmatrix) getRoomID(channel string) string {
b.RLock()
defer b.RUnlock()
for ID, name := range b.RoomMap {
if name == channel {
return ID
}
}
return ""
}
func (b *Bmatrix) handlematrix() error { func (b *Bmatrix) handlematrix() error {
warning := "Not relaying this message, please setup a dedicated bot user"
syncer := b.mc.Syncer.(*matrix.DefaultSyncer) syncer := b.mc.Syncer.(*matrix.DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *matrix.Event) { syncer.OnEventType("m.room.message", func(ev *matrix.Event) {
if ev.Content["msgtype"].(string) == "m.text" && ev.Sender != b.UserID { 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.RLock()
b.Remote <- config.Message{Username: ev.Sender, Text: ev.Content["body"].(string), Channel: ev.RoomID, Account: b.Account} channel, ok := b.RoomMap[ev.RoomID]
b.RUnlock()
if !ok {
flog.Debugf("Unknown room %s", ev.RoomID)
return
} }
if ev.Sender == b.UserID && ev.Content["body"].(string) != warning { flog.Debugf("Sending message from %s on %s to gateway", ev.Sender, b.Account)
b.mc.SendText(ev.RoomID, warning) b.Remote <- config.Message{Username: ev.Sender, Text: ev.Content["body"].(string), Channel: channel, Account: b.Account}
} }
flog.Debugf("Received: %#v", ev) flog.Debugf("Received: %#v", ev)
}) })

View File

@ -608,7 +608,7 @@ enable=true
# see (https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau) # see (https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau)
#hipchat - id_channel (see https://www.hipchat.com/account/xmpp for the correct channel) #hipchat - id_channel (see https://www.hipchat.com/account/xmpp for the correct channel)
#rocketchat - #channel (# is required) #rocketchat - #channel (# is required)
#matrix - room internal ID (looks like !QJFqjsGJwmQzbuBfff:matrix.org) #matrix - #channel:server (eg #yourchannel:matrix.org)
#REQUIRED #REQUIRED
channel="#testing" channel="#testing"