use access token when available
This commit is contained in:
parent
e8a0b29c0b
commit
4f12bdc1c9
46
main.go
46
main.go
@ -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,21 +53,40 @@ 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 err != nil {
|
if Token == "" {
|
||||||
panic(err)
|
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)
|
client.Store = NewLazyMemStore(Statefile)
|
||||||
_, err = client.Login(&mautrix.ReqLogin{
|
if Token == "" {
|
||||||
Type: "m.login.password",
|
login_res, err := client.Login(&mautrix.ReqLogin{
|
||||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
|
Type: "m.login.password",
|
||||||
Password: Password,
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
|
||||||
StoreCredentials: true,
|
Password: Password,
|
||||||
})
|
StoreCredentials: true,
|
||||||
if err != nil {
|
})
|
||||||
panic(err)
|
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 := 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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user