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

28
main.go
View File

@ -14,6 +14,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
) )
var Homeserver string var Homeserver string
@ -21,6 +22,7 @@ var Username string
var Password string var Password string
var DimensionServer string var DimensionServer string
var HomeserverDomain string var HomeserverDomain string
var Token string
var GitCommit string var GitCommit string
var GitTag string var GitTag string
var Statefile string var Statefile string
@ -42,6 +44,7 @@ func main() {
viper.SetDefault("statefile", "simpstate") viper.SetDefault("statefile", "simpstate")
Username = viper.GetString("username") Username = viper.GetString("username")
Password = viper.GetString("password") Password = viper.GetString("password")
Token = viper.GetString("access_token")
DimensionServer = viper.GetString("dimension") DimensionServer = viper.GetString("dimension")
HomeserverDomain = viper.GetString("domain") HomeserverDomain = viper.GetString("domain")
Statefile = viper.GetString("statefile") Statefile = viper.GetString("statefile")
@ -50,12 +53,22 @@ func main() {
StartTime = time.Now() StartTime = time.Now()
var vtubers []*Vtuber var vtubers []*Vtuber
log.Println("Logging into", Homeserver, "as", Username) log.Println("Logging into", Homeserver, "as", Username)
client, err := mautrix.NewClient(Homeserver, "", "") var client *mautrix.Client
if Token == "" {
client, err = mautrix.NewClient(Homeserver, "", "")
if err != nil { if err != nil {
panic(err) 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) client.Store = NewLazyMemStore(Statefile)
_, err = client.Login(&mautrix.ReqLogin{ if Token == "" {
login_res, err := client.Login(&mautrix.ReqLogin{
Type: "m.login.password", Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username}, Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
Password: Password, Password: Password,
@ -64,7 +77,16 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Login successful") 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")
}
syncer := client.Syncer.(*mautrix.DefaultSyncer) syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if evt.Sender == client.UserID { if evt.Sender == client.UserID {