aboutsummaryrefslogtreecommitdiff
path: root/db.go
blob: 42c9aeef0798f3dc4dc735403da8dc4b5a306aed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main

import (
	"fmt"
	"sync"

	"github.com/hashicorp/golang-lru"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	log "github.com/sirupsen/logrus"
	"golang.org/x/crypto/blake2b"

	"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
	"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
)

var db *gorm.DB
var dbCache *lru.TwoQueueCache

func InitDb() error {
	var err error

	db, err = gorm.Open(config.DbType, config.DbPath)
	if err != nil {
		return err
	}

	db.AutoMigrate(&DbAccountConfig{})

	db.AutoMigrate(&DbJoinedRoom{})
	db.Model(&DbJoinedRoom{}).AddIndex("idx_joined_room_user_protocol_account", "mx_user_id", "protocol", "account_name")

	db.AutoMigrate(&DbUserMap{})
	db.Model(&DbUserMap{}).AddIndex("idx_user_map_protocol_user", "protocol", "user_id")

	db.AutoMigrate(&DbRoomMap{})
	db.Model(&DbRoomMap{}).AddIndex("idx_room_map_protocol_room", "protocol", "room_id")

	db.AutoMigrate(&DbPmRoomMap{})
	db.Model(&DbPmRoomMap{}).AddIndex("idx_pm_room_map_protocol_user_account_user", "protocol", "user_id", "mx_user_id", "account_name")

	db.AutoMigrate(&DbKv{})

	dbCache, err = lru.New2Q(10000)
	if err != nil {
		return err
	}

	return nil
}

// Account configuration
type DbAccountConfig struct {
	gorm.Model

	MxUserID string `gorm:"index"`
	Name     string
	Protocol string
	Config   string
}

// List of joined channels to be re-joined on reconnect
type DbJoinedRoom struct {
	ID uint `gorm:"primary_key"`

	// User id and account name
	MxUserID    string
	Protocol    string
	AccountName string

	// Room ID
	RoomID connector.RoomID
}

// User mapping between protocol user IDs and puppeted matrix ids
type DbUserMap struct {
	ID uint `gorm:"primary_key"`

	Protocol string
	UserID   connector.UserID
	MxUserID string `gorm:"index"`
}

// Room mapping between Matrix rooms and outside rooms
type DbRoomMap struct {
	ID uint `gorm:"primary_key"`

	// Network protocol
	Protocol string

	// Room id on the bridged network
	RoomID connector.RoomID

	// Bridged room matrix id
	MxRoomID string `gorm:"index"`
}

// Room mapping between Matrix rooms and private messages
type DbPmRoomMap struct {
	ID uint `gorm:"primary_key"`

	// User id and account name of the local end viewed on Matrix
	MxUserID    string
	Protocol    string
	AccountName string

	// User id to reach them
	UserID connector.UserID

	// Bridged room for PMs
	MxRoomID string `gorm:"index"`
}

// Key-value store for various things
type DbKv struct {
	Key   string `gorm:"primary_key"`
	Value string
}

// ---- Simple locking mechanism
// Slot keys are strings that identify the object we are acting upon
// They define which lock to lock for a certain operation
// They are also used as keys in the LRU cache

var dbLocks [256]sync.Mutex

func dbLockSlot(key string) {
	slot := blake2b.Sum512([]byte(key))[0]
	dbLocks[slot].Lock()
}

func dbUnlockSlot(key string) {
	slot := blake2b.Sum512([]byte(key))[0]
	dbLocks[slot].Unlock()
}

// ---- Key-value store supporting atomic test-and-set

func dbKvSlotKey(key string) string {
	return "kv:" + key
}

func dbKvGet(key string) string {
	slot_key := dbKvSlotKey(key)

	if ent, ok := dbCache.Get(slot_key); ok {
		return ent.(string)
	}

	var entry DbKv
	if db.Where(&DbKv{Key: key}).First(&entry).RecordNotFound() {
		return ""
	} else {
		return entry.Value
	}
}

func dbKvPut(key string, value string) {
	slot_key := dbKvSlotKey(key)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	dbKvPutLocked(key, value)
}

// Variant of dbKvPut that does not take a lock,
// use this if the slot is already locked
func dbKvPutLocked(key string, value string) {
	slot_key := dbKvSlotKey(key)

	var entry DbKv
	db.Where(&DbKv{Key: key}).Assign(&DbKv{Value: value}).FirstOrCreate(&entry)
	dbCache.Add(slot_key, value)
}

func dbKvTestAndSet(key string, value string) bool {
	slot_key := dbKvSlotKey(key)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	// True if value was changed, false if was already set
	if dbKvGet(key) == value {
		return false
	}

	dbKvPutLocked(key, value)
	return true
}

// ----

func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
	slot_key := fmt.Sprintf("room:%s/%s", protocol, roomId)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	if cached, ok := dbCache.Get(slot_key); ok {
		return cached.(string), nil
	}

	// Check if room exists in our mapping,
	// If not create it
	var room DbRoomMap
	must_create := db.First(&room, DbRoomMap{
		Protocol: protocol,
		RoomID:   roomId,
	}).RecordNotFound()

	if must_create {
		alias := roomAlias(protocol, roomId)

		// Delete previous alias if it existed
		prev_full_alias := fmt.Sprintf("#%s:%s", alias, config.MatrixDomain)
		mx_room_id, err := mx.DirectoryRoom(prev_full_alias)
		if err == nil {
			mx.DirectoryDeleteRoom(prev_full_alias)
		}

		// Create room
		name := fmt.Sprintf("%s (%s)", roomId, protocol)

		mx_room_id, err = mx.CreateRoom(name, alias, []string{})
		if err != nil {
			log.Warnf("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.Tracef("%s -> %s", slot_key, room.MxRoomID)
	dbCache.Add(slot_key, room.MxRoomID)

	return room.MxRoomID, nil
}

func dbPmRoomSlotKey(room *DbPmRoomMap) string {
	return fmt.Sprintf("pmroom:%s/%s/%s/%s",
		room.Protocol, room.MxUserID, room.AccountName, room.UserID)
}

func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMxId string, usAccount string) (string, error) {
	map_key := &DbPmRoomMap{
		MxUserID:    usMxId,
		Protocol:    protocol,
		AccountName: usAccount,
		UserID:      them,
	}
	slot_key := dbPmRoomSlotKey(map_key)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	if cached, ok := dbCache.Get(slot_key); ok {
		return cached.(string), nil
	}

	var room DbPmRoomMap
	must_create := db.First(&room, map_key).RecordNotFound()

	if must_create {
		name := fmt.Sprintf("%s (%s)", them, protocol)

		mx_room_id, err := mx.CreateDirectRoomAs([]string{usMxId}, themMxId)
		if err != nil {
			log.Warnf("Could not create room for %s: %s", name, err)
			return "", err
		}

		room = DbPmRoomMap{
			MxUserID:    usMxId,
			Protocol:    protocol,
			AccountName: usAccount,
			UserID:      them,
			MxRoomID:    mx_room_id,
		}
		db.Create(&room)
	}

	log.Tracef("%s -> %s", slot_key, room.MxRoomID)
	dbCache.Add(slot_key, room.MxRoomID)

	return room.MxRoomID, nil
}

func dbDeletePmRoom(room *DbPmRoomMap) {
	slot_key := dbPmRoomSlotKey(room)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	db.Delete(room)
	dbCache.Remove(slot_key)
}

func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
	slot_key := fmt.Sprintf("user:%s/%s", protocol, userId)

	dbLockSlot(slot_key)
	defer dbUnlockSlot(slot_key)

	if cached, ok := dbCache.Get(slot_key); ok {
		return cached.(string), nil
	}

	var user DbUserMap

	must_create := db.First(&user, DbUserMap{
		Protocol: protocol,
		UserID:   userId,
	}).RecordNotFound()
	if must_create {
		username := userMxId(protocol, userId)

		err := mx.RegisterUser(username)
		if err != nil {
			if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
				log.Warnf("Could not register %s: %s", username, err)
				return "", err
			}
		}

		mxid := fmt.Sprintf("@%s:%s", username, config.MatrixDomain)
		mx.ProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))

		user = DbUserMap{
			Protocol: protocol,
			UserID:   userId,
			MxUserID: mxid,
		}
		db.Create(&user)
	}

	log.Tracef("%s -> %s", slot_key, user.MxUserID)
	dbCache.Add(slot_key, user.MxUserID)

	return user.MxUserID, nil
}

func dbIsPmRoom(mxRoomId string) *DbPmRoomMap {
	var pm_room DbPmRoomMap
	if db.First(&pm_room, DbPmRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
		return nil
	} else {
		return &pm_room
	}
}

func dbIsPublicRoom(mxRoomId string) *DbRoomMap {
	var room DbRoomMap
	if db.First(&room, DbRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
		return nil
	} else {
		return &room
	}
}