|
|
@@ -2,13 +2,12 @@ package main
|
|
|
|
|
|
import (
|
|
|
"log"
|
|
|
- "os"
|
|
|
|
|
|
+ "github.com/alecthomas/kong"
|
|
|
"github.com/ayntgl/discordo/config"
|
|
|
"github.com/ayntgl/discordo/ui"
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
"github.com/rivo/tview"
|
|
|
- "github.com/urfave/cli/v2"
|
|
|
"github.com/zalando/go-keyring"
|
|
|
)
|
|
|
|
|
|
@@ -17,63 +16,85 @@ const (
|
|
|
usage = "A lightweight, secure, and feature-rich Discord terminal client"
|
|
|
)
|
|
|
|
|
|
+var cli struct {
|
|
|
+ Token string `help:"The authentication token."`
|
|
|
+ Config string `help:"The path of the configuration file." type:"path"`
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
- t, _ := keyring.Get(name, "token")
|
|
|
-
|
|
|
- cliApp := &cli.App{
|
|
|
- Name: name,
|
|
|
- Usage: usage,
|
|
|
- EnableBashCompletion: true,
|
|
|
- Flags: []cli.Flag{
|
|
|
- &cli.StringFlag{
|
|
|
- Name: "token",
|
|
|
- Usage: "The client authentication token.",
|
|
|
- Value: t,
|
|
|
- DefaultText: "From keyring",
|
|
|
- Aliases: []string{"t"},
|
|
|
- },
|
|
|
- &cli.StringFlag{
|
|
|
- Name: "config",
|
|
|
- Usage: "The path of the configuration file.",
|
|
|
- Value: config.DefaultPath(),
|
|
|
- Aliases: []string{"c"},
|
|
|
- },
|
|
|
- },
|
|
|
+ kong.Parse(&cli, kong.Name(name), kong.Description(usage), kong.UsageOnError())
|
|
|
+
|
|
|
+ // If the authentication token is provided via a flag, store it in the default keyring.
|
|
|
+ if cli.Token != "" {
|
|
|
+ go keyring.Set(name, "token", cli.Token)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Defaults
|
|
|
+ if cli.Config == "" {
|
|
|
+ cli.Config = config.DefaultPath()
|
|
|
+ }
|
|
|
+
|
|
|
+ if cli.Token == "" {
|
|
|
+ cli.Token, _ = keyring.Get(name, "token")
|
|
|
+ }
|
|
|
+
|
|
|
+ c := config.New()
|
|
|
+ err := c.Load(cli.Config)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- cliApp.Action = func(ctx *cli.Context) error {
|
|
|
- c := config.New()
|
|
|
- err := c.Load(ctx.String("config"))
|
|
|
+ app := ui.NewApp(cli.Token, c)
|
|
|
+ if cli.Token != "" {
|
|
|
+ err := app.Connect()
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ log.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- token := ctx.String("token")
|
|
|
- app := ui.NewApp(token, c)
|
|
|
- if token != "" {
|
|
|
- err := app.Connect()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ app.DrawMainFlex()
|
|
|
+
|
|
|
+ app.SetRoot(app.MainFlex, true)
|
|
|
+ app.SetFocus(app.GuildsTree)
|
|
|
+ } else {
|
|
|
+ loginForm := ui.NewLoginForm(false)
|
|
|
+ loginForm.AddButton("Login", func() {
|
|
|
+ email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
|
|
|
+ password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
|
|
|
+ if email == "" || password == "" {
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- app.DrawMainFlex()
|
|
|
- app.SetFocus(app.GuildsTree)
|
|
|
- } else {
|
|
|
- loginForm := ui.NewLoginForm(false)
|
|
|
- loginForm.AddButton("Login", func() {
|
|
|
- email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
|
|
|
- password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
|
|
|
- if email == "" || password == "" {
|
|
|
- return
|
|
|
- }
|
|
|
+ // Login using the email and password only
|
|
|
+ lr, err := app.State.Login(email, password)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
|
|
|
- // Login using the email and password only
|
|
|
- lr, err := app.State.Login(email, password)
|
|
|
+ if lr.Token != "" && !lr.MFA {
|
|
|
+ app.State.Token = lr.Token
|
|
|
+ err = app.Connect()
|
|
|
if err != nil {
|
|
|
log.Fatal(err)
|
|
|
}
|
|
|
|
|
|
- if lr.Token != "" && !lr.MFA {
|
|
|
+ app.DrawMainFlex()
|
|
|
+ app.SetRoot(app.MainFlex, true)
|
|
|
+ app.SetFocus(app.GuildsTree)
|
|
|
+ go keyring.Set(name, "token", lr.Token)
|
|
|
+ } else {
|
|
|
+ // The account has MFA enabled, reattempt login with MFA code and ticket.
|
|
|
+ mfaLoginForm := ui.NewLoginForm(true)
|
|
|
+ mfaLoginForm.AddButton("Login", func() {
|
|
|
+ code := mfaLoginForm.GetFormItem(0).(*tview.InputField).GetText()
|
|
|
+ if code == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ lr, err = app.State.TOTP(code, lr.Ticket)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
app.State.Token = lr.Token
|
|
|
err = app.Connect()
|
|
|
if err != nil {
|
|
|
@@ -81,63 +102,37 @@ func main() {
|
|
|
}
|
|
|
|
|
|
app.DrawMainFlex()
|
|
|
+ app.SetRoot(app.MainFlex, true)
|
|
|
app.SetFocus(app.GuildsTree)
|
|
|
|
|
|
go keyring.Set(name, "token", lr.Token)
|
|
|
- } else {
|
|
|
- // The account has MFA enabled, reattempt login with MFA code and ticket.
|
|
|
- mfaLoginForm := ui.NewLoginForm(true)
|
|
|
- mfaLoginForm.AddButton("Login", func() {
|
|
|
- code := mfaLoginForm.GetFormItem(0).(*tview.InputField).GetText()
|
|
|
- if code == "" {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- lr, err = app.State.TOTP(code, lr.Ticket)
|
|
|
- if err != nil {
|
|
|
- log.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
- app.State.Token = lr.Token
|
|
|
- err = app.Connect()
|
|
|
- if err != nil {
|
|
|
- log.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
- app.DrawMainFlex()
|
|
|
- app.SetFocus(app.GuildsTree)
|
|
|
-
|
|
|
- go keyring.Set(name, "token", lr.Token)
|
|
|
- })
|
|
|
-
|
|
|
- app.SetRoot(mfaLoginForm, true)
|
|
|
- }
|
|
|
- })
|
|
|
+ })
|
|
|
|
|
|
- app.SetRoot(loginForm, true)
|
|
|
- }
|
|
|
+ app.SetRoot(mfaLoginForm, true)
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
- tview.Borders.TopLeftFocus = tview.Borders.TopLeft
|
|
|
- tview.Borders.TopRightFocus = tview.Borders.TopRight
|
|
|
- tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
|
|
|
- tview.Borders.BottomRightFocus = tview.Borders.BottomRight
|
|
|
- tview.Borders.HorizontalFocus = tview.Borders.Horizontal
|
|
|
- tview.Borders.VerticalFocus = tview.Borders.Vertical
|
|
|
- tview.Borders.TopLeft = 0
|
|
|
- tview.Borders.TopRight = 0
|
|
|
- tview.Borders.BottomLeft = 0
|
|
|
- tview.Borders.BottomRight = 0
|
|
|
- tview.Borders.Horizontal = 0
|
|
|
- tview.Borders.Vertical = 0
|
|
|
-
|
|
|
- tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(app.Config.Theme.Background)
|
|
|
- tview.Styles.BorderColor = tcell.GetColor(app.Config.Theme.Border)
|
|
|
- tview.Styles.TitleColor = tcell.GetColor(app.Config.Theme.Title)
|
|
|
-
|
|
|
- return app.Run()
|
|
|
+ app.SetRoot(loginForm, true)
|
|
|
}
|
|
|
|
|
|
- err := cliApp.Run(os.Args)
|
|
|
+ tview.Borders.TopLeftFocus = tview.Borders.TopLeft
|
|
|
+ tview.Borders.TopRightFocus = tview.Borders.TopRight
|
|
|
+ tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
|
|
|
+ tview.Borders.BottomRightFocus = tview.Borders.BottomRight
|
|
|
+ tview.Borders.HorizontalFocus = tview.Borders.Horizontal
|
|
|
+ tview.Borders.VerticalFocus = tview.Borders.Vertical
|
|
|
+ tview.Borders.TopLeft = 0
|
|
|
+ tview.Borders.TopRight = 0
|
|
|
+ tview.Borders.BottomLeft = 0
|
|
|
+ tview.Borders.BottomRight = 0
|
|
|
+ tview.Borders.Horizontal = 0
|
|
|
+ tview.Borders.Vertical = 0
|
|
|
+
|
|
|
+ tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(app.Config.Theme.Background)
|
|
|
+ tview.Styles.BorderColor = tcell.GetColor(app.Config.Theme.Border)
|
|
|
+ tview.Styles.TitleColor = tcell.GetColor(app.Config.Theme.Title)
|
|
|
+
|
|
|
+ err = app.Run()
|
|
|
if err != nil {
|
|
|
log.Fatal(err)
|
|
|
}
|