summaryrefslogtreecommitdiffstats
path: root/bridge/matrix/matrix.go
blob: 628e8d91ee7479511eb93fe086acc01b7cad1956 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bmatrix

import (
	"regexp"
	"sync"

	"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
	RoomMap map[string]string
	sync.RWMutex
}

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.RoomMap = make(map[string]string)
	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 {
	resp, err := b.mc.JoinRoom(channel, "", nil)
	if err != nil {
		return err
	}
	b.Lock()
	b.RoomMap[resp.RoomID] = channel
	b.Unlock()
	return err
}

func (b *Bmatrix) Send(msg config.Message) error {
	flog.Debugf("Receiving %#v", msg)
	channel := b.getRoomID(msg.Channel)
	flog.Debugf("Sending to channel %s", channel)
	b.mc.SendText(channel, msg.Username+msg.Text)
	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 {
	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 {
			b.RLock()
			channel, ok := b.RoomMap[ev.RoomID]
			b.RUnlock()
			if !ok {
				flog.Debugf("Unknown room %s", ev.RoomID)
				return
			}
			username := ev.Sender[1:]
			if b.Config.NoHomeServerSuffix {
				re := regexp.MustCompile("(.*?):.*")
				username = re.ReplaceAllString(username, `$1`)
			}
			flog.Debugf("Sending message from %s on %s to gateway", ev.Sender, b.Account)
			b.Remote <- config.Message{Username: username, Text: ev.Content["body"].(string), Channel: channel, Account: b.Account}
		}
		flog.Debugf("Received: %#v", ev)
	})
	go func() {
		for {
			if err := b.mc.Sync(); err != nil {
				flog.Println("Sync() returned ", err)
			}
		}
	}()
	return nil
}