aboutsummaryrefslogtreecommitdiff
path: root/account.go
diff options
context:
space:
mode:
Diffstat (limited to 'account.go')
-rw-r--r--account.go74
1 files changed, 67 insertions, 7 deletions
diff --git a/account.go b/account.go
index c17fb2f..6785fb7 100644
--- a/account.go
+++ b/account.go
@@ -2,6 +2,7 @@ package main
import (
"fmt"
+ "reflect"
"strings"
"sync"
@@ -15,22 +16,63 @@ type Account struct {
AccountName string
Protocol string
Config map[string]string
- Conn Connector
+ Conn Connector
JoinedRooms map[RoomID]bool
}
var accountsLock sync.Mutex
var registeredAccounts = map[string]map[string]*Account{}
-func AddAccount(a *Account) {
+func SetAccount(mxid string, name string, protocol string, config map[string]string) error {
accountsLock.Lock()
defer accountsLock.Unlock()
- if _, ok := registeredAccounts[a.MatrixUser]; !ok {
- registeredAccounts[a.MatrixUser] = make(map[string]*Account)
+ if _, ok := registeredAccounts[mxid]; !ok {
+ registeredAccounts[mxid] = make(map[string]*Account)
}
- registeredAccounts[a.MatrixUser][a.AccountName] = a
+ 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) {
+ prev_acct.Config = config
+ go prev_acct.connect()
+ }
+ } else {
+ conn := createConnector(protocol)
+ 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 {
+ accountsLock.Lock()
+ defer accountsLock.Unlock()
+
+ ret := []*Account{}
+ if accts, ok := registeredAccounts[mxUser]; ok {
+ for _, acct := range accts {
+ ret = append(ret, acct)
+ }
+ }
+ return ret
}
func FindAccount(mxUser string, name string) *Account {
@@ -70,6 +112,24 @@ func RemoveAccount(mxUser string, name string) {
}
}
+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)
+ }
+ }
+}
+
// ----
func (a *Account) ezbrMessagef(format string, args ...interface{}) {
@@ -78,10 +138,10 @@ func (a *Account) ezbrMessagef(format string, args ...interface{}) {
ezbrSystemSend(a.MatrixUser, msg)
}
-func (a *Account) connect(config map[string]string) {
+func (a *Account) connect() {
ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
- err := a.Conn.Configure(config)
+ err := a.Conn.Configure(a.Config)
if err != nil {
ezbrSystemSendf(a.MatrixUser, "%s (%s) cannot connect: %s", a.AccountName, a.Protocol, err.Error())
return