aboutsummaryrefslogtreecommitdiff
path: root/account.go
diff options
context:
space:
mode:
Diffstat (limited to 'account.go')
-rw-r--r--account.go65
1 files changed, 64 insertions, 1 deletions
diff --git a/account.go b/account.go
index 8da6d44..a76b782 100644
--- a/account.go
+++ b/account.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"strings"
+ "sync"
log "github.com/sirupsen/logrus"
@@ -18,17 +19,23 @@ type Account struct {
JoinedRooms map[RoomID]bool
}
+var accountsLock sync.Mutex
var registeredAccounts = map[string]map[string]*Account{}
func AddAccount(a *Account) {
+ accountsLock.Lock()
+ defer accountsLock.Unlock()
+
if _, ok := registeredAccounts[a.MatrixUser]; !ok {
registeredAccounts[a.MatrixUser] = make(map[string]*Account)
}
registeredAccounts[a.MatrixUser][a.AccountName] = a
- ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
}
func FindAccount(mxUser string, name string) *Account {
+ accountsLock.Lock()
+ defer accountsLock.Unlock()
+
if u, ok := registeredAccounts[mxUser]; ok {
if a, ok := u[name]; ok {
return a
@@ -38,6 +45,9 @@ func FindAccount(mxUser string, name string) *Account {
}
func FindJoinedAccount(mxUser string, protocol string, room RoomID) *Account {
+ accountsLock.Lock()
+ defer accountsLock.Unlock()
+
if u, ok := registeredAccounts[mxUser]; ok {
for _, acct := range u {
if acct.Protocol == protocol {
@@ -51,17 +61,55 @@ func FindJoinedAccount(mxUser string, protocol string, room RoomID) *Account {
}
func RemoveAccount(mxUser string, name string) {
+ accountsLock.Lock()
+ defer accountsLock.Unlock()
+
if u, ok := registeredAccounts[mxUser]; ok {
delete(u, name)
}
}
+// ----
+
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)
}
+func (a *Account) connect(config map[string]string, join_rooms []string) {
+ ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
+
+ err := a.Conn.Configure(config)
+ if err != nil {
+ ezbrSystemSendf(a.MatrixUser, "%s (%s) cannot connect: %s", a.AccountName, a.Protocol, err.Error())
+ return
+ }
+
+ for _, room := range join_rooms {
+ var entry DbJoinedRoom
+ db.Where(&DbJoinedRoom{
+ MxUserID: a.MatrixUser,
+ Protocol: a.Protocol,
+ AccountName: a.AccountName,
+ RoomID: RoomID(room),
+ }).FirstOrCreate(&entry)
+ }
+
+ 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())
+ }
+ }
+}
+
// ---- Begin event handlers ----
func (a *Account) Joined(roomId RoomID) {
@@ -69,6 +117,14 @@ func (a *Account) Joined(roomId RoomID) {
if err != nil {
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err.Error())
}
+
+ var entry DbJoinedRoom
+ db.Where(&DbJoinedRoom{
+ MxUserID: a.MatrixUser,
+ Protocol: a.Protocol,
+ AccountName: a.AccountName,
+ RoomID: roomId,
+ }).FirstOrCreate(&entry)
}
func (a *Account) joinedInternal(roomId RoomID) error {
@@ -95,6 +151,13 @@ func (a *Account) Left(roomId RoomID) {
if err != nil {
a.ezbrMessagef("Dropping Account.Left %s: %s", roomId, err.Error())
}
+
+ db.Where(&DbJoinedRoom{
+ MxUserID: a.MatrixUser,
+ Protocol: a.Protocol,
+ AccountName: a.AccountName,
+ RoomID: roomId,
+ }).Delete(&DbJoinedRoom{})
}
func (a *Account) leftInternal(roomId RoomID) error {