diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..aae01d4 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,30 @@ +package config + +import ( + "encoding/json" + "os" +) + +type Config struct { + Matrix MatrixConfig +} + +type MatrixConfig struct { + HomeserverURL string + UserIdentifier string + Password string +} + +func ParseFromFile(path string) (config *Config, err error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + err = json.Unmarshal(data, config) + if err != nil { + return nil, err + } + + return config, nil +} diff --git a/internal/matrix/client.go b/internal/matrix/client.go index d91cd57..7cce71b 100644 --- a/internal/matrix/client.go +++ b/internal/matrix/client.go @@ -1,9 +1,29 @@ package matrix import ( + "git.luj0ga.de/franconian/matrix-pretix/internal/config" + "maunium.net/go/mautrix" ) -type Client struct { - client *mautrix.Client +func NewClient(config *config.MatrixConfig) (*mautrix.Client, error) { + client, err := mautrix.NewClient(config.HomeserverURL, "", "") + if err != nil { + return nil, err + } + + _, err := client.Login(&mautrix.ReqLogin{ + Type: mautrix.AuthTypePassword, + Identifier: mautrix.UserIdentifier{ + Type: mautrix.IdentifierTypeUser, + User: config.UserIdentifier, + }, + Password: config.Password, + StoreCredentials: true, + }) + if err != nil { + return nil, err + } + + return client, nil } diff --git a/internal/matrix/login.go b/internal/matrix/login.go deleted file mode 100644 index ee268ba..0000000 --- a/internal/matrix/login.go +++ /dev/null @@ -1,8 +0,0 @@ -package matrix - -import "fmt" - -func Login(identifier, password string) error { - fmt.Println(identifier, password) - return nil -} diff --git a/main.go b/main.go index 3a32b7b..46995f1 100644 --- a/main.go +++ b/main.go @@ -1,47 +1,24 @@ package main import ( - "bufio" - "fmt" - "os" + "log" + "git.luj0ga.de/franconian/matrix-pretix/internal/config" "git.luj0ga.de/franconian/matrix-pretix/internal/matrix" - "git.luj0ga.de/franconian/matrix-pretix/internal/util" ) -func login() { - prompter := util.NewPrompter(bufio.NewScanner(os.Stdin)) - - identifier, err := prompter.Prompt("user id") - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } - - password, err := util.PromptForPassword("password") - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } - - err = matrix.Login(identifier, password) - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - -func run() { -} - func main() { - switch { - case len(os.Args) == 2 && os.Args[1] == "login": - login() - case len(os.Args) == 1: - run() - default: - fmt.Fprintf(os.Stderr, "usage: %s [ \"\" | \"login\" ]\n", os.Args[0]) - os.Exit(1) + if len(os.Args) != 2 { + log.Fatal("missing config file path") + } + + config, err := config.ParseFromFile(os.Args[1]) + if err != nil { + log.Fatal(err) + } + + client, err := matrix.NewClient(&config.Matrix) + if err != nil { + log.Fatal(err) } }