From 4f12bdc1c93b90ce7504acd29bd395b7f36e2307 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 8 Dec 2021 13:22:56 -0500 Subject: [PATCH] use access token when available --- main.go | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index a1f129f..5643aab 100644 --- a/main.go +++ b/main.go @@ -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 {