matrix-pretix/internal/matrix/client.go

167 lines
3.4 KiB
Go
Raw Normal View History

2022-07-22 19:52:41 +02:00
package matrix
import (
2022-07-24 02:40:44 +02:00
"context"
"database/sql"
"log"
"sync"
"git.luj0ga.de/franconian/matrix-pretix/internal/config"
2022-07-24 02:40:44 +02:00
"git.luj0ga.de/franconian/matrix-pretix/internal/database"
2022-07-22 19:52:41 +02:00
"maunium.net/go/mautrix"
2022-07-24 02:40:44 +02:00
"maunium.net/go/mautrix/crypto"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
2022-07-22 19:52:41 +02:00
)
2022-07-24 02:40:44 +02:00
type Client struct {
client *mautrix.Client
config *config.MatrixConfig
db *sql.DB
deviceID id.DeviceID
olmMachine *crypto.OlmMachine
store *sqlStore
syncer *mautrix.DefaultSyncer
userID id.UserID
}
func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) {
client, err := mautrix.NewClient(config.HomeserverURL, "", "")
if err != nil {
return nil, err
}
2022-07-24 02:40:44 +02:00
syncer := mautrix.NewDefaultSyncer()
client.Syncer = syncer
store := &sqlStore{db}
client.Store = store
err = store.CreateTables()
if err != nil {
return nil, err
}
userID, err := makeUserID(config.UserIdentifier, config.HomeserverURL)
if err != nil {
return nil, err
}
deviceID := loadDeviceID(db, userID)
return &Client{client, config, db, deviceID, nil, store, syncer, userID}, nil
}
func (c *Client) Login() error {
c.syncer.OnEventType(event.StateMember, c.handleMemberEvent)
_, err := c.client.Login(&mautrix.ReqLogin{
Type: mautrix.AuthTypePassword,
Identifier: mautrix.UserIdentifier{
Type: mautrix.IdentifierTypeUser,
2022-07-24 02:40:44 +02:00
User: c.config.UserIdentifier,
},
2022-07-24 02:40:44 +02:00
Password: c.config.Password,
DeviceID: c.deviceID,
InitialDeviceDisplayName: c.config.DisplayName,
StoreCredentials: true,
})
if err != nil {
2022-07-24 02:40:44 +02:00
return err
}
return nil
}
func (c *Client) Encrypt() error {
sqlCryptoStore := crypto.NewSQLCryptoStore(
c.db,
database.DBDriverName,
c.userID.String(),
c.deviceID,
[]byte(c.config.PickleKey),
logger{},
)
err := sqlCryptoStore.CreateTables()
if err != nil {
return err
}
c.olmMachine = crypto.NewOlmMachine(c.client, &logger{}, sqlCryptoStore, c.store)
err = c.olmMachine.Load()
if err != nil {
return err
}
c.syncer.OnSync(c.olmMachine.ProcessSyncResponse)
c.syncer.OnEventType(event.StateEncryption, c.handleEncryptionEvent)
return nil
}
func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
err := c.client.SyncWithContext(ctx)
if err != nil && err != ctx.Err() {
log.Print(err)
}
}
}
}
func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event) {
if c.olmMachine != nil {
c.olmMachine.HandleMemberEvent(evt)
}
c.store.SetMembership(evt.RoomID, evt.GetStateKey(), evt.Content.AsMember().Membership)
if evt.GetStateKey() == c.userID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
allowed := false
for _, room := range c.config.AllowedRooms {
if room == evt.RoomID.String() {
allowed = true
break
}
}
if allowed {
_, err := c.client.JoinRoomByID(evt.RoomID)
if err != nil {
log.Print(err)
}
} else {
_, err := c.client.LeaveRoom(evt.RoomID)
if err != nil {
log.Print(err)
}
}
}
}
func (c *Client) handleEncryptionEvent(source mautrix.EventSource, evt *event.Event) {
c.store.SetEncryptionEvent(evt.RoomID, evt.Content.AsEncryption())
}
func loadDeviceID(db *sql.DB, accountID id.UserID) (deviceID id.DeviceID) {
row := db.QueryRow("SELECT device_id FROM crypto_account WHERE account_id = ?;", accountID)
err := row.Scan(&deviceID)
if err != nil {
return ""
}
2022-07-24 02:40:44 +02:00
return deviceID
2022-07-22 19:52:41 +02:00
}