2020-02-16 18:30:49 +00:00
|
|
|
package appservice
|
|
|
|
|
|
|
|
import (
|
2020-02-16 21:07:41 +00:00
|
|
|
"fmt"
|
2020-02-17 18:02:00 +00:00
|
|
|
"strings"
|
2020-02-17 08:41:08 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-02-16 21:07:41 +00:00
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2020-02-17 18:02:26 +00:00
|
|
|
MatrixUser string
|
2020-02-16 18:30:49 +00:00
|
|
|
AccountName string
|
2020-02-17 18:02:26 +00:00
|
|
|
Protocol string
|
|
|
|
Conn Connector
|
2020-02-16 22:27:03 +00:00
|
|
|
|
|
|
|
JoinedRooms map[RoomID]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var registeredAccounts = map[string]map[string]*Account{}
|
|
|
|
|
|
|
|
func AddAccount(a *Account) {
|
|
|
|
if _, ok := registeredAccounts[a.MatrixUser]; !ok {
|
|
|
|
registeredAccounts[a.MatrixUser] = make(map[string]*Account)
|
|
|
|
}
|
|
|
|
registeredAccounts[a.MatrixUser][a.AccountName] = a
|
2020-02-17 20:04:21 +00:00
|
|
|
ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindAccount(mxUser string, name string) *Account {
|
|
|
|
if u, ok := registeredAccounts[mxUser]; ok {
|
|
|
|
if a, ok := u[name]; ok {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindJoinedAccount(mxUser string, protocol string, room RoomID) *Account {
|
|
|
|
if u, ok := registeredAccounts[mxUser]; ok {
|
|
|
|
for _, acct := range u {
|
|
|
|
if acct.Protocol == protocol {
|
|
|
|
if j, ok := acct.JoinedRooms[room]; ok && j {
|
|
|
|
return acct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveAccount(mxUser string, name string) {
|
|
|
|
if u, ok := registeredAccounts[mxUser]; ok {
|
|
|
|
delete(u, name)
|
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:52:50 +00:00
|
|
|
func (a *Account) ezbrMessagef(format string, args ...interface{}) {
|
|
|
|
msg := fmt.Sprintf(format, args...)
|
|
|
|
msg = fmt.Sprintf("%s: %s", a.Protocol, msg)
|
|
|
|
ezbrSystemSend(a.MatrixUser, msg)
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
// ---- Begin event handlers ----
|
2020-02-16 22:27:03 +00:00
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
func (a *Account) Joined(roomId RoomID) {
|
2020-02-17 18:02:00 +00:00
|
|
|
err := a.joinedInternal(roomId)
|
|
|
|
if err != nil {
|
2020-02-17 18:52:50 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err)
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) joinedInternal(roomId RoomID) error {
|
2020-02-16 22:27:03 +00:00
|
|
|
a.JoinedRooms[roomId] = true
|
|
|
|
|
2020-02-16 21:07:41 +00:00
|
|
|
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
log.Debugf("Joined %s (%s)\n", roomId, a.MatrixUser)
|
2020-02-16 21:07:41 +00:00
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
err = mx.RoomInvite(mx_room_id, a.MatrixUser)
|
2020-02-17 18:02:00 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "already in the room") {
|
|
|
|
err = nil
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
// ----
|
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
func (a *Account) Left(roomId RoomID) {
|
2020-02-17 18:02:00 +00:00
|
|
|
err := a.leftInternal(roomId)
|
|
|
|
if err != nil {
|
2020-02-17 18:52:50 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Left %s: %s", roomId, err)
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) leftInternal(roomId RoomID) error {
|
2020-02-16 22:27:03 +00:00
|
|
|
delete(a.JoinedRooms, roomId)
|
|
|
|
|
2020-02-16 21:07:41 +00:00
|
|
|
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 20:04:21 +00:00
|
|
|
log.Printf("Left %s (%s)\n", roomId, a.MatrixUser)
|
2020-02-16 21:07:41 +00:00
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
err = mx.RoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
|
2020-02-17 20:04:21 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "not in the room") {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
|
|
|
|
func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
|
|
|
|
err := a.userInfoUpdatedInternal(user, info)
|
2020-02-16 21:07:41 +00:00
|
|
|
if err != nil {
|
2020-02-17 18:52:50 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.UserInfoUpdated %s: %s", user, err)
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
func (a *Account) userInfoUpdatedInternal(user UserID, info *UserInfo) error {
|
2020-02-17 15:28:32 +00:00
|
|
|
mx_user_id, err := dbGetMxUser(a.Protocol, user)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-17 15:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.DisplayName != "" {
|
2020-02-21 13:27:42 +00:00
|
|
|
err2 := mx.ProfileDisplayname(mx_user_id, fmt.Sprintf("%s (%s)", info.DisplayName, a.Protocol))
|
2020-02-17 18:02:00 +00:00
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
2020-02-17 15:28:32 +00:00
|
|
|
}
|
2020-02-17 18:02:00 +00:00
|
|
|
|
2020-02-17 15:28:32 +00:00
|
|
|
if info.Avatar != nil {
|
2020-02-21 17:43:47 +00:00
|
|
|
cache_key := fmt.Sprintf("%s/user_avatar/%s", a.Protocol, user)
|
|
|
|
cache_val := info.Avatar.Filename()
|
|
|
|
if cache_val == "" || dbCacheTestAndSet(cache_key, cache_val) {
|
|
|
|
err2 := mx.ProfileAvatar(mx_user_id, info.Avatar)
|
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
2020-02-17 15:28:32 +00:00
|
|
|
}
|
2020-02-17 18:02:00 +00:00
|
|
|
|
|
|
|
return err
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
// ----
|
|
|
|
|
2020-02-17 14:30:01 +00:00
|
|
|
func (a *Account) RoomInfoUpdated(roomId RoomID, author UserID, info *RoomInfo) {
|
2020-02-17 18:02:00 +00:00
|
|
|
err := a.roomInfoUpdatedInternal(roomId, author, info)
|
|
|
|
if err != nil {
|
2020-02-17 18:52:50 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.RoomInfoUpdated %s: %s", roomId, err)
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) roomInfoUpdatedInternal(roomId RoomID, author UserID, info *RoomInfo) error {
|
2020-02-17 14:30:01 +00:00
|
|
|
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:28:32 +00:00
|
|
|
as_mxid := ezbrMxId()
|
2020-02-17 14:30:01 +00:00
|
|
|
if len(author) > 0 {
|
2020-02-17 18:02:00 +00:00
|
|
|
mx_user_id, err2 := dbGetMxUser(a.Protocol, author)
|
|
|
|
if err2 == nil {
|
2020-02-17 14:30:01 +00:00
|
|
|
as_mxid = mx_user_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Topic != "" {
|
2020-02-21 13:27:42 +00:00
|
|
|
err2 := mx.RoomTopicAs(mx_room_id, info.Topic, as_mxid)
|
2020-02-17 18:02:00 +00:00
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
2020-02-17 14:30:01 +00:00
|
|
|
}
|
2020-02-17 15:28:32 +00:00
|
|
|
|
|
|
|
if info.Name != "" {
|
2020-02-17 18:52:50 +00:00
|
|
|
name := fmt.Sprintf("%s (%s)", info.Name, a.Protocol)
|
2020-02-21 13:27:42 +00:00
|
|
|
err2 := mx.RoomNameAs(mx_room_id, name, as_mxid)
|
2020-02-17 18:02:00 +00:00
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
2020-02-17 15:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.Picture != nil {
|
2020-02-21 17:43:47 +00:00
|
|
|
cache_key := fmt.Sprintf("%s/room_picture/%s", a.Protocol, roomId)
|
|
|
|
cache_val := info.Picture.Filename()
|
|
|
|
if cache_val == "" || dbCacheTestAndSet(cache_key, cache_val) {
|
|
|
|
err2 := mx.RoomAvatarAs(mx_room_id, info.Picture, as_mxid)
|
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
2020-02-21 17:08:40 +00:00
|
|
|
}
|
2020-02-17 15:28:32 +00:00
|
|
|
}
|
2020-02-17 18:02:00 +00:00
|
|
|
|
|
|
|
return err
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:02:00 +00:00
|
|
|
// ----
|
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
func (a *Account) Event(event *Event) {
|
2020-02-17 18:02:00 +00:00
|
|
|
err := a.eventInternal(event)
|
|
|
|
if err != nil {
|
2020-02-17 18:52:50 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Event %s %s %s: %s", event.Author, event.Recipient, event.Room, err)
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) eventInternal(event *Event) error {
|
2020-02-21 17:43:47 +00:00
|
|
|
// TODO: automatically ignore events that come from one of our bridged matrix users
|
|
|
|
// TODO: deduplicate events if we have several matrix users joined the same room (hard problem)
|
|
|
|
|
2020-02-16 21:07:41 +00:00
|
|
|
mx_user_id, err := dbGetMxUser(a.Protocol, event.Author)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if event.Type == EVENT_JOIN {
|
|
|
|
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
|
|
|
|
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
err = mx.RoomInvite(mx_room_id, mx_user_id)
|
2020-02-16 21:07:41 +00:00
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
if strings.Contains(err.Error(), "already in the room") {
|
2020-02-21 18:28:00 +00:00
|
|
|
return nil
|
2020-02-17 18:02:00 +00:00
|
|
|
}
|
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
return mx.RoomJoinAs(mx_room_id, mx_user_id)
|
2020-02-16 21:07:41 +00:00
|
|
|
} else if event.Type == EVENT_LEAVE {
|
|
|
|
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
|
|
|
|
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
|
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:27:42 +00:00
|
|
|
return mx.RoomLeaveAs(mx_room_id, mx_user_id)
|
2020-02-16 21:57:30 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
|
|
|
|
mx_room_id := ""
|
|
|
|
|
2020-02-16 21:07:41 +00:00
|
|
|
if len(event.Room) > 0 {
|
2020-02-16 21:57:30 +00:00
|
|
|
mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room)
|
2020-02-16 21:07:41 +00:00
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
2020-02-16 21:57:30 +00:00
|
|
|
} else {
|
|
|
|
mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName)
|
2020-02-16 21:07:41 +00:00
|
|
|
if err != nil {
|
2020-02-17 18:02:00 +00:00
|
|
|
return err
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
2020-02-16 21:57:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:28:00 +00:00
|
|
|
if event.Id != "" {
|
|
|
|
cache_key := fmt.Sprintf("%s/event_seen/%s/%s",
|
|
|
|
a.Protocol, mx_room_id, event.Id)
|
|
|
|
if !dbCacheTestAndSet(cache_key, "yes") {
|
|
|
|
// false: cache key was not modified, meaning we
|
|
|
|
// already saw the event
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 21:57:30 +00:00
|
|
|
typ := "m.text"
|
|
|
|
if event.Type == EVENT_ACTION {
|
|
|
|
typ = "m.emote"
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:57:53 +00:00
|
|
|
err = mx.SendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:08:40 +00:00
|
|
|
if event.Attachments != nil {
|
|
|
|
for _, file := range event.Attachments {
|
2020-02-21 14:57:53 +00:00
|
|
|
mxfile, err := mx.UploadMedia(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content := map[string]interface{} {
|
|
|
|
"body": mxfile.Filename(),
|
|
|
|
"filename": mxfile.Filename(),
|
|
|
|
"url": fmt.Sprintf("mxc://%s/%s", mxfile.MxcServer, mxfile.MxcMediaId),
|
|
|
|
}
|
|
|
|
if sz := mxfile.ImageSize(); sz != nil {
|
|
|
|
content["msgtype"] = "m.image"
|
|
|
|
content["info"] = map[string]interface{} {
|
|
|
|
"mimetype": mxfile.Mimetype(),
|
|
|
|
"size": mxfile.Size(),
|
2020-02-21 17:08:40 +00:00
|
|
|
"w": sz.Width,
|
|
|
|
"h": sz.Height,
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content["msgtype"] = "m.file"
|
|
|
|
content["info"] = map[string]interface{} {
|
|
|
|
"mimetype": mxfile.Mimetype(),
|
|
|
|
"size": mxfile.Size(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = mx.SendAs(mx_room_id, "m.room.message", content, mx_user_id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-02-16 21:07:41 +00:00
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|