use access token when available
This commit is contained in:
parent
e8a0b29c0b
commit
4f12bdc1c9
28
main.go
28
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,12 +53,22 @@ func main() {
|
||||
StartTime = time.Now()
|
||||
var vtubers []*Vtuber
|
||||
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 {
|
||||
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{
|
||||
if Token == "" {
|
||||
login_res, err := client.Login(&mautrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
|
||||
Password: Password,
|
||||
@ -64,7 +77,16 @@ func main() {
|
||||
if err != nil {
|
||||
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.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||
if evt.Sender == client.UserID {
|
||||
|
Loading…
Reference in New Issue
Block a user