2017-02-19 23:49:27 +00:00
|
|
|
package bmatrix
|
|
|
|
|
|
|
|
import (
|
2017-11-21 22:50:27 +00:00
|
|
|
"bytes"
|
2020-08-23 22:12:30 +00:00
|
|
|
"encoding/json"
|
2018-02-24 14:54:43 +00:00
|
|
|
"fmt"
|
2019-01-06 21:25:19 +00:00
|
|
|
"html"
|
2017-11-21 22:50:27 +00:00
|
|
|
"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"
|
2020-08-23 22:12:30 +00:00
|
|
|
"time"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2020-08-30 12:01:52 +00:00
|
|
|
matrix "github.com/matrix-org/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
|
2020-08-20 20:41:53 +00:00
|
|
|
htmlTag *regexp.Regexp
|
|
|
|
htmlReplacementTag *regexp.Regexp
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2017-02-19 23:49:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:12:30 +00:00
|
|
|
type httpError struct {
|
|
|
|
Errcode string `json:"errcode"`
|
|
|
|
Err string `json:"error"`
|
|
|
|
RetryAfterMs int `json:"retry_after_ms"`
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
b := &Bmatrix{Config: cfg}
|
2019-02-17 20:48:32 +00:00
|
|
|
b.htmlTag = regexp.MustCompile("</.*?>")
|
2020-08-20 20:41:53 +00:00
|
|
|
b.htmlReplacementTag = regexp.MustCompile("<[^>]*>")
|
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
|
2018-03-04 22:52:14 +00:00
|
|
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
|
|
|
b.mc, err = matrix.NewClient(b.GetString("Server"), "", "")
|
2017-02-19 23:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := b.mc.Login(&matrix.ReqLogin{
|
2020-08-30 12:01:52 +00:00
|
|
|
Type: "m.login.password",
|
|
|
|
User: b.GetString("Login"),
|
|
|
|
Password: b.GetString("Password"),
|
|
|
|
Identifier: matrix.NewUserIdentifier(b.GetString("Login")),
|
2017-02-19 23:49:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.mc.SetCredentials(resp.UserID, resp.AccessToken)
|
|
|
|
b.UserID = resp.UserID
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connection succeeded")
|
2017-02-19 23:49:27 +00:00
|
|
|
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 {
|
2020-08-23 22:12:30 +00:00
|
|
|
retry:
|
2017-08-12 12:51:41 +00:00
|
|
|
resp, err := b.mc.JoinRoom(channel.Name, "", nil)
|
2017-02-24 16:58:51 +00:00
|
|
|
if err != nil {
|
2020-08-23 22:12:30 +00:00
|
|
|
httpErr := handleError(err)
|
|
|
|
if httpErr.Errcode == "M_LIMIT_EXCEEDED" {
|
|
|
|
b.Log.Infof("getting ratelimited by matrix, sleeping approx %d seconds before joining %s", httpErr.RetryAfterMs/1000, channel.Name)
|
|
|
|
time.Sleep((time.Duration(httpErr.RetryAfterMs) * time.Millisecond))
|
|
|
|
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:58:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-23 22:12:30 +00:00
|
|
|
|
2017-02-24 16:58:51 +00:00
|
|
|
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()
|
2020-08-23 22:12:30 +00:00
|
|
|
|
|
|
|
return nil
|
2017-02-19 23:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Bmatrix) Send(msg config.Message) (string, error) {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-24 14:54:43 +00:00
|
|
|
|
2017-12-24 23:55:39 +00:00
|
|
|
channel := b.getRoomID(msg.Channel)
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Channel %s maps to channel id %s", msg.Channel, channel)
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Make a action /me of the message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventUserAction {
|
2018-11-07 19:36:50 +00:00
|
|
|
m := matrix.TextMessage{
|
|
|
|
MsgType: "m.emote",
|
|
|
|
Body: msg.Username + msg.Text,
|
|
|
|
}
|
|
|
|
resp, err := b.mc.SendMessageEvent(channel, "m.room.message", m)
|
2017-12-24 23:55:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, err
|
2017-09-11 20:45:15 +00:00
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Delete message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2018-02-24 14:54:43 +00:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
resp, err := b.mc.RedactEvent(channel, msg.ID, &matrix.ReqRedact{})
|
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
|
|
|
|
2018-02-24 14:54:43 +00:00
|
|
|
// Upload a file if it exists
|
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) {
|
2018-12-03 15:51:11 +00:00
|
|
|
if _, err := b.mc.SendText(channel, rmsg.Username+rmsg.Text); err != nil {
|
|
|
|
b.Log.Errorf("sendText failed: %s", err)
|
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
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 {
|
2018-12-03 15:51:11 +00:00
|
|
|
return b.handleUploadFiles(&msg, channel)
|
2017-11-21 22:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 14:54:43 +00:00
|
|
|
// Edit message if we have an ID
|
|
|
|
// matrix has no editing support
|
|
|
|
|
2019-02-16 17:36:09 +00:00
|
|
|
// Use notices to send join/leave events
|
|
|
|
if msg.Event == config.EventJoinLeave {
|
|
|
|
resp, err := b.mc.SendNotice(channel, msg.Username+msg.Text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, err
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:37:41 +00:00
|
|
|
if b.GetBool("HTMLDisable") {
|
|
|
|
resp, err := b.mc.SendText(channel, msg.Username+msg.Text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, err
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:41:53 +00:00
|
|
|
var username string
|
|
|
|
var plainUsername string
|
2019-02-17 20:48:32 +00:00
|
|
|
// check if we have a </tag>. if we have, we don't escape HTML. #696
|
|
|
|
if b.htmlTag.MatchString(msg.Username) {
|
|
|
|
username = msg.Username
|
2020-08-20 20:41:53 +00:00
|
|
|
// remove the HTML formatting for beautiful push messages #1188
|
|
|
|
plainUsername = b.htmlReplacementTag.ReplaceAllString(msg.Username, "")
|
|
|
|
} else {
|
|
|
|
username = html.EscapeString(msg.Username)
|
|
|
|
plainUsername = msg.Username
|
2019-02-17 20:48:32 +00:00
|
|
|
}
|
2020-08-20 20:41:53 +00:00
|
|
|
|
2019-01-06 21:25:19 +00:00
|
|
|
// Post normal message with HTML support (eg riot.im)
|
2020-08-30 12:01:52 +00:00
|
|
|
resp, err := b.mc.SendFormattedText(channel, plainUsername+msg.Text, username+helper.ParseMarkdown(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
|
|
|
|
2018-11-07 23:29:30 +00:00
|
|
|
func (b *Bmatrix) handlematrix() {
|
2017-02-19 23:49:27 +00:00
|
|
|
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 {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Println("Sync() returned ", err)
|
2017-02-19 23:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-12-24 23:55:39 +00:00
|
|
|
|
|
|
|
func (b *Bmatrix) handleEvent(ev *matrix.Event) {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("== Receiving event: %#v", ev)
|
2017-12-24 23:55:39 +00:00
|
|
|
if ev.Sender != b.UserID {
|
|
|
|
b.RLock()
|
|
|
|
channel, ok := b.RoomMap[ev.RoomID]
|
|
|
|
b.RUnlock()
|
|
|
|
if !ok {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Unknown room %s", ev.RoomID)
|
2017-12-24 23:55:39 +00:00
|
|
|
return
|
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Create our message
|
2020-02-09 23:06:54 +00:00
|
|
|
rmsg := config.Message{
|
|
|
|
Username: ev.Sender[1:],
|
|
|
|
Channel: channel,
|
|
|
|
Account: b.Account,
|
|
|
|
UserID: ev.Sender,
|
|
|
|
ID: ev.ID,
|
|
|
|
Avatar: b.getAvatarURL(ev.Sender),
|
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Text must be a string
|
|
|
|
if rmsg.Text, ok = ev.Content["body"].(string); !ok {
|
2018-11-07 19:36:50 +00:00
|
|
|
b.Log.Errorf("Content[body] is not a string: %T\n%#v",
|
|
|
|
ev.Content["body"], ev.Content)
|
2018-02-24 14:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove homeserver suffix if configured
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetBool("NoHomeServerSuffix") {
|
2017-12-24 23:55:39 +00:00
|
|
|
re := regexp.MustCompile("(.*?):.*")
|
2018-02-24 14:54:43 +00:00
|
|
|
rmsg.Username = re.ReplaceAllString(rmsg.Username, `$1`)
|
2017-12-24 23:55:39 +00:00
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Delete event
|
2017-12-24 23:55:39 +00:00
|
|
|
if ev.Type == "m.room.redaction" {
|
2018-11-15 19:43:43 +00:00
|
|
|
rmsg.Event = config.EventMsgDelete
|
2017-12-24 23:55:39 +00:00
|
|
|
rmsg.ID = ev.Redacts
|
2018-11-15 19:43:43 +00:00
|
|
|
rmsg.Text = config.EventMsgDelete
|
2017-12-24 23:55:39 +00:00
|
|
|
b.Remote <- rmsg
|
|
|
|
return
|
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Do we have a /me action
|
2017-12-24 23:55:39 +00:00
|
|
|
if ev.Content["msgtype"].(string) == "m.emote" {
|
2018-11-15 19:43:43 +00:00
|
|
|
rmsg.Event = config.EventUserAction
|
2017-12-24 23:55:39 +00:00
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
// Do we have attachments
|
|
|
|
if b.containsAttachment(ev.Content) {
|
|
|
|
err := b.handleDownloadFile(&rmsg, ev.Content)
|
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Errorf("download failed: %#v", err)
|
2018-02-24 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", ev.Sender, b.Account)
|
2018-02-24 14:54:43 +00:00
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDownloadFile handles file download
|
|
|
|
func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]interface{}) error {
|
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
url, name, msgtype, mtype string
|
|
|
|
info map[string]interface{}
|
|
|
|
size float64
|
|
|
|
)
|
|
|
|
|
|
|
|
rmsg.Extra = make(map[string][]interface{})
|
|
|
|
if url, ok = content["url"].(string); !ok {
|
|
|
|
return fmt.Errorf("url isn't a %T", url)
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1)
|
2018-02-24 14:54:43 +00:00
|
|
|
|
|
|
|
if info, ok = content["info"].(map[string]interface{}); !ok {
|
|
|
|
return fmt.Errorf("info isn't a %T", info)
|
|
|
|
}
|
|
|
|
if size, ok = info["size"].(float64); !ok {
|
|
|
|
return fmt.Errorf("size isn't a %T", size)
|
|
|
|
}
|
|
|
|
if name, ok = content["body"].(string); !ok {
|
|
|
|
return fmt.Errorf("name isn't a %T", name)
|
|
|
|
}
|
|
|
|
if msgtype, ok = content["msgtype"].(string); !ok {
|
|
|
|
return fmt.Errorf("msgtype isn't a %T", msgtype)
|
|
|
|
}
|
|
|
|
if mtype, ok = info["mimetype"].(string); !ok {
|
|
|
|
return fmt.Errorf("mtype isn't a %T", mtype)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have an image uploaded without extension
|
|
|
|
if !strings.Contains(name, ".") {
|
|
|
|
if msgtype == "m.image" {
|
|
|
|
mext, _ := mime.ExtensionsByType(mtype)
|
|
|
|
if len(mext) > 0 {
|
2018-11-07 23:46:34 +00:00
|
|
|
name += mext[0]
|
2018-02-24 14:54:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// just a default .png extension if we don't have mime info
|
2018-11-07 23:46:34 +00:00
|
|
|
name += ".png"
|
2018-02-24 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the size is ok
|
2018-02-26 23:33:21 +00:00
|
|
|
err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General)
|
2018-02-24 14:54:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// actually download the file
|
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("download %s failed %#v", url, err)
|
|
|
|
}
|
|
|
|
// add the downloaded data to the message
|
2018-02-26 23:33:21 +00:00
|
|
|
helper.HandleDownloadData(b.Log, rmsg, name, "", url, data, b.General)
|
2018-02-24 14:54:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-03 15:51:11 +00:00
|
|
|
// handleUploadFiles handles native upload of files.
|
|
|
|
func (b *Bmatrix) handleUploadFiles(msg *config.Message, channel string) (string, error) {
|
2018-02-24 14:54:43 +00:00
|
|
|
for _, f := range msg.Extra["file"] {
|
2018-12-03 15:51:11 +00:00
|
|
|
if fi, ok := f.(config.FileInfo); ok {
|
|
|
|
b.handleUploadFile(msg, channel, &fi)
|
2017-12-24 23:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-24 14:54:43 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-12-03 15:51:11 +00:00
|
|
|
// handleUploadFile handles native upload of a file.
|
|
|
|
func (b *Bmatrix) handleUploadFile(msg *config.Message, channel string, fi *config.FileInfo) {
|
|
|
|
content := bytes.NewReader(*fi.Data)
|
|
|
|
sp := strings.Split(fi.Name, ".")
|
|
|
|
mtype := mime.TypeByExtension("." + sp[len(sp)-1])
|
|
|
|
if fi.Comment != "" {
|
|
|
|
_, err := b.mc.SendText(channel, msg.Username+fi.Comment)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("file comment failed: %#v", err)
|
|
|
|
}
|
2019-02-16 17:35:36 +00:00
|
|
|
} else {
|
|
|
|
// image and video uploads send no username, we have to do this ourself here #715
|
|
|
|
_, err := b.mc.SendText(channel, msg.Username)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("file comment failed: %#v", err)
|
|
|
|
}
|
2018-12-03 15:51:11 +00:00
|
|
|
}
|
|
|
|
b.Log.Debugf("uploading file: %s %s", fi.Name, mtype)
|
|
|
|
res, err := b.mc.UploadToContentRepo(content, mtype, int64(len(*fi.Data)))
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("file upload failed: %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.Contains(mtype, "video"):
|
|
|
|
b.Log.Debugf("sendVideo %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendVideo(channel, fi.Name, res.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("sendVideo failed: %#v", err)
|
|
|
|
}
|
|
|
|
case strings.Contains(mtype, "image"):
|
|
|
|
b.Log.Debugf("sendImage %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendImage(channel, fi.Name, res.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("sendImage failed: %#v", err)
|
|
|
|
}
|
2020-09-26 19:28:24 +00:00
|
|
|
case strings.Contains(mtype, "audio"):
|
|
|
|
b.Log.Debugf("sendAudio %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendMessageEvent(channel, "m.room.message", matrix.AudioMessage{
|
|
|
|
MsgType: "m.audio",
|
2020-08-30 12:01:52 +00:00
|
|
|
Body: fi.Name,
|
|
|
|
URL: res.ContentURI,
|
2020-09-26 19:28:24 +00:00
|
|
|
Info: matrix.AudioInfo{
|
2020-08-30 12:01:52 +00:00
|
|
|
Mimetype: mtype,
|
|
|
|
Size: uint(len(*fi.Data)),
|
|
|
|
},
|
|
|
|
})
|
2019-10-26 22:06:44 +00:00
|
|
|
if err != nil {
|
2020-09-26 19:28:24 +00:00
|
|
|
b.Log.Errorf("sendAudio failed: %#v", err)
|
2019-10-26 22:06:44 +00:00
|
|
|
}
|
2020-09-26 19:28:24 +00:00
|
|
|
default:
|
|
|
|
b.Log.Debugf("sendFile %s", res.ContentURI)
|
|
|
|
_, err = b.mc.SendMessageEvent(channel, "m.room.message", matrix.FileMessage{
|
|
|
|
MsgType: "m.file",
|
2020-08-30 12:01:52 +00:00
|
|
|
Body: fi.Name,
|
|
|
|
URL: res.ContentURI,
|
2020-09-26 19:28:24 +00:00
|
|
|
Info: matrix.FileInfo{
|
2020-08-30 12:01:52 +00:00
|
|
|
Mimetype: mtype,
|
|
|
|
Size: uint(len(*fi.Data)),
|
|
|
|
},
|
|
|
|
})
|
2019-10-26 22:06:44 +00:00
|
|
|
if err != nil {
|
2020-09-26 19:28:24 +00:00
|
|
|
b.Log.Errorf("sendFile failed: %#v", err)
|
2019-10-26 22:06:44 +00:00
|
|
|
}
|
2018-12-03 15:51:11 +00:00
|
|
|
}
|
|
|
|
b.Log.Debugf("result: %#v", res)
|
|
|
|
}
|
|
|
|
|
2018-02-24 14:54:43 +00:00
|
|
|
// skipMessages returns true if this message should not be handled
|
|
|
|
func (b *Bmatrix) containsAttachment(content map[string]interface{}) bool {
|
|
|
|
// Skip empty messages
|
|
|
|
if content["msgtype"] == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only allow image,video or file msgtypes
|
|
|
|
if !(content["msgtype"].(string) == "m.image" ||
|
|
|
|
content["msgtype"].(string) == "m.video" ||
|
|
|
|
content["msgtype"].(string) == "m.file") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2017-12-24 23:55:39 +00:00
|
|
|
}
|
2020-02-09 23:06:54 +00:00
|
|
|
|
|
|
|
// getAvatarURL returns the avatar URL of the specified sender
|
|
|
|
func (b *Bmatrix) getAvatarURL(sender string) string {
|
2020-08-30 12:01:52 +00:00
|
|
|
urlPath := b.mc.BuildURL("profile", sender, "avatar_url")
|
|
|
|
|
|
|
|
s := struct {
|
|
|
|
AvatarURL string `json:"avatar_url"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := b.mc.MakeRequest("GET", urlPath, nil, &s)
|
2020-02-09 23:06:54 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("getAvatarURL failed: %s", err)
|
|
|
|
return ""
|
|
|
|
}
|
2020-08-30 12:01:52 +00:00
|
|
|
url := strings.ReplaceAll(s.AvatarURL, "mxc://", b.GetString("Server")+"/_matrix/media/r0/thumbnail/")
|
2020-05-10 22:21:56 +00:00
|
|
|
if url != "" {
|
|
|
|
url += "?width=37&height=37&method=crop"
|
|
|
|
}
|
2020-02-09 23:06:54 +00:00
|
|
|
return url
|
|
|
|
}
|
2020-08-23 22:12:30 +00:00
|
|
|
|
|
|
|
func handleError(err error) *httpError {
|
|
|
|
mErr, ok := err.(matrix.HTTPError)
|
|
|
|
if !ok {
|
|
|
|
return &httpError{
|
|
|
|
Err: "not a HTTPError",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var httpErr httpError
|
|
|
|
|
|
|
|
if err := json.Unmarshal(mErr.Contents, &httpErr); err != nil {
|
|
|
|
return &httpError{
|
|
|
|
Err: "unmarshal failed",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpErr
|
|
|
|
}
|