aboutsummaryrefslogtreecommitdiff
path: root/appservice/account.go
diff options
context:
space:
mode:
Diffstat (limited to 'appservice/account.go')
-rw-r--r--appservice/account.go155
1 files changed, 45 insertions, 110 deletions
diff --git a/appservice/account.go b/appservice/account.go
index 230860a..553165a 100644
--- a/appservice/account.go
+++ b/appservice/account.go
@@ -4,7 +4,6 @@ import (
"fmt"
"log"
- "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
@@ -13,9 +12,52 @@ type Account struct {
AccountName string
Protocol string
Conn Connector
+
+ 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
+}
+
+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)
+ }
}
+// ----
+
func (a *Account) Joined(roomId RoomID) {
+ a.JoinedRooms[roomId] = true
+
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
@@ -30,6 +72,8 @@ func (a *Account) Joined(roomId RoomID) {
}
func (a *Account) Left(roomId RoomID) {
+ delete(a.JoinedRooms, roomId)
+
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
@@ -112,112 +156,3 @@ func (a *Account) Event(event *Event) {
}
}
-// ----
-
-func dbGetMxRoom(protocol string, roomId RoomID) (string, error) {
- var room DbRoomMap
-
- // Check if room exists in our mapping,
- // If not create it
- must_create := db.First(&room, DbRoomMap{
- Protocol: protocol,
- RoomID: roomId,
- }).RecordNotFound()
- if must_create {
- alias := roomAlias(protocol, roomId)
- // Lookup alias
- mx_room_id, err := mxDirectoryRoom(fmt.Sprintf("#%s:%s", alias, config.MatrixDomain))
-
- // If no alias found, create room
- if err != nil {
- name := fmt.Sprintf("%s (%s)", roomId, protocol)
-
- mx_room_id, err = mxCreateRoom(name, alias, []string{})
- if err != nil {
- log.Printf("Could not create room for %s: %s", name, err)
- return "", err
- }
- }
-
- room = DbRoomMap{
- Protocol: protocol,
- RoomID: roomId,
- MxRoomID: mx_room_id,
- }
- db.Create(&room)
- }
- log.Printf("Got room id: %s", room.MxRoomID)
-
- return room.MxRoomID, nil
-}
-
-func dbGetMxPmRoom(protocol string, them UserID, themMxId string, usMxId string, usAccount string) (string, error) {
- var room DbPmRoomMap
-
- must_create := db.First(&room, DbPmRoomMap{
- MxUserID: usMxId,
- Protocol: protocol,
- AccountName: usAccount,
- UserID: them,
- }).RecordNotFound()
- if must_create {
- name := fmt.Sprintf("%s (%s)", them, protocol)
-
- mx_room_id, err := mxCreateDirectRoomAs(name, []string{usMxId}, themMxId)
- if err != nil {
- log.Printf("Could not create room for %s: %s", name, err)
- return "", err
- }
-
- err = mxRoomJoinAs(mx_room_id, themMxId)
- if err != nil {
- log.Printf("Could not join %s as %s", mx_room_id, themMxId)
- return "", err
- }
-
- room = DbPmRoomMap{
- MxUserID: usMxId,
- Protocol: protocol,
- AccountName: usAccount,
- UserID: them,
- MxRoomID: mx_room_id,
- }
- db.Create(&room)
- }
- log.Printf("Got PM room id: %s", room.MxRoomID)
-
- return room.MxRoomID, nil
-}
-
-func dbGetMxUser(protocol string, userId UserID) (string, error) {
- var user DbUserMap
-
- must_create := db.First(&user, DbUserMap{
- Protocol: protocol,
- UserID: userId,
- }).RecordNotFound()
- if must_create {
- username := userMxId(protocol, userId)
-
- err := mxRegisterUser(username)
- if err != nil {
- if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
- log.Printf("Could not register %s: %s", username, err)
- return "", err
- }
- }
-
- mxid := fmt.Sprintf("@%s:%s", username, config.MatrixDomain)
- mxProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))
-
- user = DbUserMap{
- Protocol: protocol,
- UserID: userId,
- MxUserID: mxid,
- }
- db.Create(&user)
- }
-
- return user.MxUserID, nil
-}
-