1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00
jfa-go/matrix_crypto.go

62 lines
1.3 KiB
Go
Raw Normal View History

//go:build e2ee
// +build e2ee
2021-07-16 13:33:51 +00:00
package main
import (
"context"
2021-07-16 13:33:51 +00:00
_ "github.com/mattn/go-sqlite3"
"maunium.net/go/mautrix/crypto/cryptohelper"
2021-07-16 13:33:51 +00:00
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
2021-07-16 16:11:17 +00:00
type Crypto struct {
helper *cryptohelper.CryptoHelper
2021-07-16 16:11:17 +00:00
}
func BuildTagsE2EE() {
buildTags = append(buildTags, "e2ee")
}
func MatrixE2EE() bool { return true }
func InitMatrixCrypto(d *MatrixDaemon) error {
2021-07-16 13:33:51 +00:00
d.Encryption = d.app.config.Section("matrix").Key("encryption").MustBool(false)
if !d.Encryption {
// return fmt.Errorf("encryption disabled")
return nil
2021-07-16 13:33:51 +00:00
}
2021-07-16 13:33:51 +00:00
dbPath := d.app.config.Section("files").Key("matrix_sql").String()
var err error
d.crypto = &Crypto{}
d.crypto.helper, err = cryptohelper.NewCryptoHelper(d.bot, []byte("jfa-go"), dbPath)
2021-07-16 13:33:51 +00:00
if err != nil {
return err
2021-07-16 13:33:51 +00:00
}
err = d.crypto.helper.Init(context.TODO())
2021-07-16 13:33:51 +00:00
if err != nil {
return err
2021-07-16 16:11:17 +00:00
}
2021-07-16 13:33:51 +00:00
d.bot.Crypto = d.crypto.helper
2021-07-16 13:33:51 +00:00
d.Encryption = true
return nil
2021-07-16 13:33:51 +00:00
}
func EncryptRoom(d *MatrixDaemon, roomID id.RoomID) error {
2021-07-16 13:33:51 +00:00
if !d.Encryption {
return nil
2021-07-16 13:33:51 +00:00
}
_, err := d.bot.SendStateEvent(context.TODO(), roomID, event.StateEncryption, "", event.EncryptionEventContent{
2021-07-16 13:33:51 +00:00
Algorithm: id.AlgorithmMegolmV1,
RotationPeriodMillis: 7 * 24 * 60 * 60 * 1000,
RotationPeriodMessages: 100,
})
return err
2021-07-16 13:33:51 +00:00
}