use access token when available
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
stryan 2021-12-08 13:22:56 -05:00
parent e8a0b29c0b
commit 4f12bdc1c9

46
main.go
View File

@ -14,6 +14,7 @@ import (
"github.com/spf13/viper"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var Homeserver string
@ -21,6 +22,7 @@ var Username string
var Password string
var DimensionServer string
var HomeserverDomain string
var Token string
var GitCommit string
var GitTag string
var Statefile string
@ -42,6 +44,7 @@ func main() {
viper.SetDefault("statefile", "simpstate")
Username = viper.GetString("username")
Password = viper.GetString("password")
Token = viper.GetString("access_token")
DimensionServer = viper.GetString("dimension")
HomeserverDomain = viper.GetString("domain")
Statefile = viper.GetString("statefile")
@ -50,21 +53,40 @@ func main() {
StartTime = time.Now()
var vtubers []*Vtuber
log.Println("Logging into", Homeserver, "as", Username)
client, err := mautrix.NewClient(Homeserver, "", "")
if err != nil {
panic(err)
var client *mautrix.Client
if Token == "" {
client, err = mautrix.NewClient(Homeserver, "", "")
if err != nil {
panic(err)
}
} else {
log.Println("using token login")
client, err = mautrix.NewClient(Homeserver, id.NewUserID(Username, HomeserverDomain), Token)
if err != nil {
panic(err)
}
}
client.Store = NewLazyMemStore(Statefile)
_, err = client.Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
Password: Password,
StoreCredentials: true,
})
if err != nil {
panic(err)
if Token == "" {
login_res, err := client.Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
Password: Password,
StoreCredentials: true,
})
if err != nil {
panic(err)
}
Token = login_res.AccessToken
viper.Set("access_token", Token)
log.Println("Login succesful, saving access_token to config file")
err = viper.WriteConfig()
if err != nil {
panic(err)
}
} else {
log.Println("skipping login since token provided")
}
fmt.Println("Login successful")
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if evt.Sender == client.UserID {