2020-02-26 15:07:33 +00:00
|
|
|
package main
|
2020-02-16 18:30:49 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-16 21:07:41 +00:00
|
|
|
"fmt"
|
2020-02-26 20:36:35 +00:00
|
|
|
"reflect"
|
2020-02-17 18:02:00 +00:00
|
|
|
"strings"
|
2020-02-26 15:30:10 +00:00
|
|
|
"sync"
|
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"
|
2020-02-28 09:18:47 +00:00
|
|
|
|
|
|
|
// Necessary for them to register their protocols
|
|
|
|
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
|
|
|
|
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/mattermost"
|
|
|
|
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
|
2020-02-16 18:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2020-02-26 19:21:32 +00:00
|
|
|
Config map[string]string
|
2020-02-16 22:27:03 +00:00
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
Conn Connector
|
2020-02-16 22:27:03 +00:00
|
|
|
JoinedRooms map[RoomID]bool
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:30:10 +00:00
|
|
|
var accountsLock sync.Mutex
|
2020-02-16 22:27:03 +00:00
|
|
|
var registeredAccounts = map[string]map[string]*Account{}
|
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
func SetAccount(mxid string, name string, protocol string, config map[string]string) error {
|
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
|
|
|
if _, ok := registeredAccounts[mxid]; !ok {
|
|
|
|
registeredAccounts[mxid] = make(map[string]*Account)
|
|
|
|
}
|
|
|
|
accounts := registeredAccounts[mxid]
|
|
|
|
|
|
|
|
if prev_acct, ok := accounts[name]; ok {
|
|
|
|
if protocol != prev_acct.Protocol {
|
|
|
|
return fmt.Errorf("Wrong protocol")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config, prev_acct.Config) {
|
2020-02-26 22:01:34 +00:00
|
|
|
prev_acct.Conn.Close()
|
|
|
|
prev_acct.JoinedRooms = map[RoomID]bool{}
|
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
prev_acct.Config = config
|
|
|
|
go prev_acct.connect()
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-28 09:18:47 +00:00
|
|
|
proto, ok := Protocols[protocol]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Invalid protocol: %s", protocol)
|
|
|
|
}
|
|
|
|
conn := proto.NewConnector()
|
2020-02-26 20:36:35 +00:00
|
|
|
if conn == nil {
|
|
|
|
return fmt.Errorf("Could not create connector for protocol %s", protocol)
|
|
|
|
}
|
|
|
|
account := &Account{
|
|
|
|
MatrixUser: mxid,
|
|
|
|
AccountName: name,
|
|
|
|
Protocol: protocol,
|
|
|
|
Config: config,
|
|
|
|
Conn: conn,
|
|
|
|
JoinedRooms: map[RoomID]bool{},
|
|
|
|
}
|
|
|
|
conn.SetHandler(account)
|
|
|
|
|
|
|
|
accounts[name] = account
|
|
|
|
go account.connect()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListAccounts(mxUser string) []*Account {
|
2020-02-26 15:30:10 +00:00
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
ret := []*Account{}
|
|
|
|
if accts, ok := registeredAccounts[mxUser]; ok {
|
|
|
|
for _, acct := range accts {
|
|
|
|
ret = append(ret, acct)
|
|
|
|
}
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
2020-02-26 20:36:35 +00:00
|
|
|
return ret
|
2020-02-16 22:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindAccount(mxUser string, name string) *Account {
|
2020-02-26 15:30:10 +00:00
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
2020-02-16 22:27:03 +00:00
|
|
|
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 {
|
2020-02-26 15:30:10 +00:00
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
2020-02-16 22:27:03 +00:00
|
|
|
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) {
|
2020-02-26 15:30:10 +00:00
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
2020-02-16 22:27:03 +00:00
|
|
|
if u, ok := registeredAccounts[mxUser]; ok {
|
2020-02-26 22:08:25 +00:00
|
|
|
if acct, ok := u[name]; ok {
|
|
|
|
acct.Conn.Close()
|
|
|
|
}
|
2020-02-16 22:27:03 +00:00
|
|
|
delete(u, name)
|
|
|
|
}
|
2020-02-16 18:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 21:49:27 +00:00
|
|
|
// ----
|
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
func SaveDbAccounts(mxid string, key *[32]byte) {
|
|
|
|
accountsLock.Lock()
|
|
|
|
defer accountsLock.Unlock()
|
|
|
|
|
|
|
|
if accounts, ok := registeredAccounts[mxid]; ok {
|
|
|
|
for name, acct := range accounts {
|
|
|
|
var entry DbAccountConfig
|
|
|
|
db.Where(&DbAccountConfig{
|
|
|
|
MxUserID: mxid,
|
|
|
|
Name: name,
|
|
|
|
}).Assign(&DbAccountConfig{
|
|
|
|
Protocol: acct.Protocol,
|
|
|
|
Config: encryptAccountConfig(acct.Config, key),
|
|
|
|
}).FirstOrCreate(&entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:49:27 +00:00
|
|
|
func LoadDbAccounts(mxid string, key *[32]byte) {
|
|
|
|
var allAccounts []DbAccountConfig
|
|
|
|
db.Where(&DbAccountConfig{MxUserID: mxid}).Find(&allAccounts)
|
|
|
|
for _, acct := range allAccounts {
|
|
|
|
config, err := decryptAccountConfig(acct.Config, key)
|
|
|
|
if err != nil {
|
2020-02-28 17:29:10 +00:00
|
|
|
ezbrSystemSendf("Could not decrypt stored configuration for account %s (%s)", acct.Name, acct.Protocol)
|
2020-02-26 21:49:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = SetAccount(mxid, acct.Name, acct.Protocol, config)
|
|
|
|
if err != nil {
|
2020-02-28 21:08:18 +00:00
|
|
|
ezbrSystemSendf(mxid, "Could not setup account %s: %s", acct.Name, err.Error())
|
2020-02-26 21:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:30:10 +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-26 20:36:35 +00:00
|
|
|
func (a *Account) connect() {
|
2020-02-26 15:30:10 +00:00
|
|
|
ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
|
|
|
|
|
2020-02-26 20:36:35 +00:00
|
|
|
err := a.Conn.Configure(a.Config)
|
2020-02-26 15:30:10 +00:00
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(a.MatrixUser, "%s (%s) cannot connect: %s", a.AccountName, a.Protocol, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var autojoin []DbJoinedRoom
|
|
|
|
db.Where(&DbJoinedRoom{
|
|
|
|
MxUserID: a.MatrixUser,
|
|
|
|
Protocol: a.Protocol,
|
|
|
|
AccountName: a.AccountName,
|
|
|
|
}).Find(&autojoin)
|
|
|
|
for _, aj := range autojoin {
|
|
|
|
err := a.Conn.Join(aj.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
ezbrSystemSendf(a.MatrixUser, "%s (%s) cannot join %s: %s", a.AccountName, a.Protocol, aj.RoomID, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:32:58 +00:00
|
|
|
func (a *Account) addAutojoin(roomId RoomID) {
|
|
|
|
var entry DbJoinedRoom
|
|
|
|
db.Where(&DbJoinedRoom{
|
|
|
|
MxUserID: a.MatrixUser,
|
|
|
|
Protocol: a.Protocol,
|
|
|
|
AccountName: a.AccountName,
|
|
|
|
RoomID: roomId,
|
|
|
|
}).FirstOrCreate(&entry)
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:16:59 +00:00
|
|
|
func (a *Account) delAutojoin(roomId RoomID) {
|
|
|
|
db.Where(&DbJoinedRoom{
|
|
|
|
MxUserID: a.MatrixUser,
|
|
|
|
Protocol: a.Protocol,
|
|
|
|
AccountName: a.AccountName,
|
|
|
|
RoomID: roomId,
|
|
|
|
}).Delete(&DbJoinedRoom{})
|
|
|
|
}
|
|
|
|
|
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-21 18:50:55 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err.Error())
|
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-28 11:16:59 +00:00
|
|
|
a.addAutojoin(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-27 09:35:09 +00:00
|
|
|
log.Tracef("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-21 18:50:55 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Left %s: %s", roomId, err.Error())
|
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-28 11:16:59 +00:00
|
|
|
a.delAutojoin(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-27 09:35:09 +00:00
|
|
|
log.Tracef("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-21 18:50:55 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.UserInfoUpdated %s: %s", user, err.Error())
|
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()
|
2020-02-28 09:34:22 +00:00
|
|
|
if cache_val == "" || dbKvTestAndSet(cache_key, cache_val) {
|
2020-02-21 17:43:47 +00:00
|
|
|
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-21 18:50:55 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.RoomInfoUpdated %s: %s", roomId, err.Error())
|
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-26 16:03:49 +00:00
|
|
|
if author == a.Conn.User() {
|
|
|
|
as_mxid = a.MatrixUser
|
|
|
|
} else 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()
|
2020-02-28 09:34:22 +00:00
|
|
|
if cache_val == "" || dbKvTestAndSet(cache_key, cache_val) {
|
2020-02-21 17:43:47 +00:00
|
|
|
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-21 18:50:55 +00:00
|
|
|
a.ezbrMessagef("Dropping Account.Event %s %s %s: %s", event.Author, event.Recipient, event.Room, err.Error())
|
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 {
|
2020-02-27 09:35:09 +00:00
|
|
|
log.Tracef("%s join %s %s", a.Protocol, event.Author, event.Room)
|
2020-02-16 21:07:41 +00:00
|
|
|
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 {
|
2020-02-27 09:35:09 +00:00
|
|
|
log.Tracef("%s join %s %s", a.Protocol, event.Author, event.Room)
|
2020-02-16 21:07:41 +00:00
|
|
|
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 {
|
2020-02-27 09:35:09 +00:00
|
|
|
log.Tracef("%s msg %s %s", a.Protocol, event.Author, event.Room)
|
2020-02-16 21:57:30 +00:00
|
|
|
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)
|
2020-02-28 09:34:22 +00:00
|
|
|
if !dbKvTestAndSet(cache_key, "yes") {
|
2020-02-21 18:28:00 +00:00
|
|
|
// 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
|
|
|
|
}
|
2020-02-23 19:24:50 +00:00
|
|
|
content := map[string]interface{}{
|
|
|
|
"body": mxfile.Filename(),
|
2020-02-21 14:57:53 +00:00
|
|
|
"filename": mxfile.Filename(),
|
2020-02-23 19:24:50 +00:00
|
|
|
"url": fmt.Sprintf("mxc://%s/%s", mxfile.MxcServer, mxfile.MxcMediaId),
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
if sz := mxfile.ImageSize(); sz != nil {
|
|
|
|
content["msgtype"] = "m.image"
|
2020-02-23 19:24:50 +00:00
|
|
|
content["info"] = map[string]interface{}{
|
2020-02-21 14:57:53 +00:00
|
|
|
"mimetype": mxfile.Mimetype(),
|
2020-02-23 19:24:50 +00:00
|
|
|
"size": mxfile.Size(),
|
|
|
|
"w": sz.Width,
|
|
|
|
"h": sz.Height,
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content["msgtype"] = "m.file"
|
2020-02-23 19:24:50 +00:00
|
|
|
content["info"] = map[string]interface{}{
|
2020-02-21 14:57:53 +00:00
|
|
|
"mimetype": mxfile.Mimetype(),
|
2020-02-23 19:24:50 +00:00
|
|
|
"size": mxfile.Size(),
|
2020-02-21 14:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2020-02-21 18:36:16 +00:00
|
|
|
|
|
|
|
// ----
|
|
|
|
|
|
|
|
func (a *Account) CacheGet(key string) string {
|
|
|
|
cache_key := fmt.Sprintf("%s/account/%s/%s/%s",
|
|
|
|
a.Protocol, a.MatrixUser, a.AccountName, key)
|
2020-02-28 09:34:22 +00:00
|
|
|
return dbKvGet(cache_key)
|
2020-02-21 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) CachePut(key string, value string) {
|
|
|
|
cache_key := fmt.Sprintf("%s/account/%s/%s/%s",
|
|
|
|
a.Protocol, a.MatrixUser, a.AccountName, key)
|
2020-02-28 09:34:22 +00:00
|
|
|
dbKvPut(cache_key, value)
|
2020-02-21 18:36:16 +00:00
|
|
|
}
|