2017-02-19 23:49:27 +00:00
|
|
|
package bmatrix
|
|
|
|
|
|
|
|
import (
|
2017-11-21 22:50:27 +00:00
|
|
|
"bytes"
|
|
|
|
"mime"
|
2017-05-12 21:04:58 +00:00
|
|
|
"regexp"
|
2017-11-21 22:50:27 +00:00
|
|
|
"strings"
|
2017-05-12 21:04:58 +00:00
|
|
|
"sync"
|
|
|
|
|
2017-02-19 23:49:27 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-21 23:28:40 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2018-02-20 22:41:09 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-12-24 23:55:39 +00:00
|
|
|
matrix "github.com/matterbridge/gomatrix"
|
2017-02-19 23:49:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bmatrix struct {
|
|
|
|
mc *matrix.Client
|
|
|
|
UserID string
|
2017-02-24 16:58:51 +00:00
|
|
|
RoomMap map[string]string
|
|
|
|
sync.RWMutex
|
2017-12-19 22:15:03 +00:00
|
|
|
*config.BridgeConfig
|
2017-02-19 23:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "matrix"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:15:03 +00:00
|
|
|
func New(cfg *config.BridgeConfig) *Bmatrix {
|
|
|
|
b := &Bmatrix{BridgeConfig: cfg}
|
2017-02-24 16:58:51 +00:00
|
|
|
b.RoomMap = make(map[string]string)
|
2017-02-19 23:49:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Bmatrix) JoinChannel(channel config.ChannelInfo) error {
|
|
|
|
resp, err := b.mc.JoinRoom(channel.Name, "", nil)
|
2017-02-24 16:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Lock()
|
2017-08-12 12:51:41 +00:00
|
|
|
b.RoomMap[resp.RoomID] = channel.Name
|
2017-02-24 16:58:51 +00:00
|
|
|
b.Unlock()
|
2017-02-19 23:49:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Bmatrix) Send(msg config.Message) (string, error) {
|
2017-02-19 23:49:27 +00:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2017-12-24 23:55:39 +00:00
|
|
|
channel := b.getRoomID(msg.Channel)
|
2017-09-11 20:45:15 +00:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
2017-12-24 23:55:39 +00:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
resp, err := b.mc.RedactEvent(channel, msg.ID, &matrix.ReqRedact{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, err
|
2017-09-11 20:45:15 +00:00
|
|
|
}
|
2017-02-24 16:58:51 +00:00
|
|
|
flog.Debugf("Sending to channel %s", channel)
|
2017-07-30 15:48:23 +00:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
2017-12-24 23:55:39 +00:00
|
|
|
resp, err := b.mc.SendMessageEvent(channel, "m.room.message",
|
2017-07-30 15:48:23 +00:00
|
|
|
matrix.TextMessage{"m.emote", msg.Username + msg.Text})
|
2018-01-09 22:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
return resp.EventID, err
|
2017-07-30 15:48:23 +00:00
|
|
|
}
|
2017-11-21 22:50:27 +00:00
|
|
|
|
|
|
|
if msg.Extra != nil {
|
2018-02-03 00:11:11 +00:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
|
|
|
b.mc.SendText(channel, rmsg.Username+rmsg.Text)
|
|
|
|
}
|
2017-11-21 22:50:27 +00:00
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
content := bytes.NewReader(*fi.Data)
|
|
|
|
sp := strings.Split(fi.Name, ".")
|
|
|
|
mtype := mime.TypeByExtension("." + sp[len(sp)-1])
|
|
|
|
if strings.Contains(mtype, "image") ||
|
|
|
|
strings.Contains(mtype, "video") {
|
2018-01-31 23:41:09 +00:00
|
|
|
if fi.Comment != "" {
|
2018-02-01 00:01:25 +00:00
|
|
|
_, err := b.mc.SendText(channel, msg.Username+fi.Comment)
|
2018-01-31 23:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("file comment failed: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 22:50:27 +00:00
|
|
|
flog.Debugf("uploading file: %s %s", fi.Name, mtype)
|
|
|
|
res, err := b.mc.UploadToContentRepo(content, mtype, int64(len(*fi.Data)))
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("file upload failed: %#v", err)
|
2017-12-11 11:25:28 +00:00
|
|
|
continue
|
2017-11-21 22:50:27 +00:00
|
|
|
}
|
|
|
|
if strings.Contains(mtype, "video") {
|
|
|
|
flog.Debugf("sendVideo %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendVideo(channel, fi.Name, res.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("sendVideo failed: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.Contains(mtype, "image") {
|
|
|
|
flog.Debugf("sendImage %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendImage(channel, fi.Name, res.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("sendImage failed: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flog.Debugf("result: %#v", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 23:55:39 +00:00
|
|
|
resp, err := b.mc.SendText(channel, msg.Username+msg.Text)
|
2018-01-09 22:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
return resp.EventID, err
|
2017-02-19 23:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 16:58:51 +00:00
|
|
|
func (b *Bmatrix) getRoomID(channel string) string {
|
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
|
|
|
for ID, name := range b.RoomMap {
|
|
|
|
if name == channel {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
|
2017-02-19 23:49:27 +00:00
|
|
|
func (b *Bmatrix) handlematrix() error {
|
|
|
|
syncer := b.mc.Syncer.(*matrix.DefaultSyncer)
|
2017-12-24 23:55:39 +00:00
|
|
|
syncer.OnEventType("m.room.redaction", b.handleEvent)
|
|
|
|
syncer.OnEventType("m.room.message", b.handleEvent)
|
2017-02-19 23:49:27 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if err := b.mc.Sync(); err != nil {
|
|
|
|
flog.Println("Sync() returned ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
|
|
|
|
func (b *Bmatrix) handleEvent(ev *matrix.Event) {
|
|
|
|
flog.Debugf("Received: %#v", ev)
|
|
|
|
if 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`)
|
|
|
|
}
|
|
|
|
var text string
|
|
|
|
text, _ = ev.Content["body"].(string)
|
|
|
|
rmsg := config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: ev.Sender}
|
|
|
|
rmsg.ID = ev.ID
|
|
|
|
if ev.Type == "m.room.redaction" {
|
|
|
|
rmsg.Event = config.EVENT_MSG_DELETE
|
|
|
|
rmsg.ID = ev.Redacts
|
|
|
|
rmsg.Text = config.EVENT_MSG_DELETE
|
|
|
|
b.Remote <- rmsg
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ev.Content["msgtype"].(string) == "m.emote" {
|
|
|
|
rmsg.Event = config.EVENT_USER_ACTION
|
|
|
|
}
|
|
|
|
if ev.Content["msgtype"] != nil && ev.Content["msgtype"].(string) == "m.image" ||
|
|
|
|
ev.Content["msgtype"].(string) == "m.video" ||
|
|
|
|
ev.Content["msgtype"].(string) == "m.file" {
|
|
|
|
flog.Debugf("ev: %#v", ev)
|
|
|
|
rmsg.Extra = make(map[string][]interface{})
|
|
|
|
url := ev.Content["url"].(string)
|
|
|
|
url = strings.Replace(url, "mxc://", b.Config.Server+"/_matrix/media/v1/download/", -1)
|
|
|
|
info := ev.Content["info"].(map[string]interface{})
|
|
|
|
size := info["size"].(float64)
|
|
|
|
name := ev.Content["body"].(string)
|
2018-01-20 17:19:17 +00:00
|
|
|
// check if we have an image uploaded without extension
|
|
|
|
if !strings.Contains(name, ".") {
|
|
|
|
if ev.Content["msgtype"].(string) == "m.image" {
|
|
|
|
if mtype, ok := ev.Content["mimetype"].(string); ok {
|
|
|
|
mext, _ := mime.ExtensionsByType(mtype)
|
|
|
|
if len(mext) > 0 {
|
|
|
|
name = name + mext[0]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// just a default .png extension if we don't have mime info
|
|
|
|
name = name + ".png"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
flog.Debugf("trying to download %#v with size %#v", name, size)
|
|
|
|
if size <= float64(b.General.MediaDownloadSize) {
|
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("download %s failed %#v", url, err)
|
|
|
|
} else {
|
|
|
|
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url))
|
|
|
|
rmsg.Extra["file"] = append(rmsg.Extra["file"], config.FileInfo{Name: name, Data: data})
|
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
} else {
|
|
|
|
flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, b.General.MediaDownloadSize)
|
|
|
|
rmsg.Event = config.EVENT_FILE_FAILURE_SIZE
|
|
|
|
rmsg.Extra[rmsg.Event] = append(rmsg.Extra[rmsg.Event], config.FileInfo{Name: name, Size: int64(size)})
|
2017-12-24 23:55:39 +00:00
|
|
|
}
|
|
|
|
rmsg.Text = ""
|
|
|
|
}
|
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", ev.Sender, b.Account)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
}
|