main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "flag"
  4. "github.com/ayntgl/astatine"
  5. "github.com/ayntgl/discordo/config"
  6. "github.com/ayntgl/discordo/ui"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/rivo/tview"
  9. "github.com/zalando/go-keyring"
  10. )
  11. const name = "discordo"
  12. func main() {
  13. var token string
  14. var cfg string
  15. flag.StringVar(&token, "token", "", "The authentication token.")
  16. flag.StringVar(&cfg, "config", "", "The path of the configuration file.")
  17. flag.Parse()
  18. if token == "" {
  19. token, _ = keyring.Get(name, "token")
  20. }
  21. c := config.New()
  22. if cfg != "" {
  23. c.Path = cfg
  24. }
  25. c.Load()
  26. app := ui.NewApp(c)
  27. if token != "" {
  28. app.Session = astatine.New(token)
  29. err := app.Connect()
  30. if err != nil {
  31. panic(err)
  32. }
  33. app.DrawMainFlex()
  34. app.SetFocus(app.GuildsList)
  35. } else {
  36. app.Session = astatine.New(token)
  37. loginForm := ui.NewLoginForm(false)
  38. loginForm.AddButton("Login", func() {
  39. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  40. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  41. if email == "" || password == "" {
  42. return
  43. }
  44. // Login using the email and password
  45. lr, err := app.Session.Login(email, password)
  46. if err != nil {
  47. panic(err)
  48. }
  49. if lr.Token != "" && !lr.Mfa {
  50. app.Session.Identify.Token = lr.Token
  51. err = app.Connect()
  52. if err != nil {
  53. panic(err)
  54. }
  55. app.DrawMainFlex()
  56. app.SetFocus(app.GuildsList)
  57. go keyring.Set(name, "token", lr.Token)
  58. } else {
  59. // The account has MFA enabled, reattempt login with MFA code and ticket.
  60. mfaLoginForm := ui.NewLoginForm(true)
  61. mfaLoginForm.AddButton("Login", func() {
  62. code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  63. if code == "" {
  64. return
  65. }
  66. lr, err = app.Session.Totp(code, lr.Ticket)
  67. if err != nil {
  68. panic(err)
  69. }
  70. app.Session.Identify.Token = lr.Token
  71. err = app.Connect()
  72. if err != nil {
  73. panic(err)
  74. }
  75. app.DrawMainFlex()
  76. app.SetFocus(app.GuildsList)
  77. go keyring.Set(name, "token", lr.Token)
  78. })
  79. }
  80. })
  81. app.SetRoot(loginForm, true)
  82. }
  83. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  84. tview.Borders.TopRightFocus = tview.Borders.TopRight
  85. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  86. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  87. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  88. tview.Borders.VerticalFocus = tview.Borders.Vertical
  89. tview.Borders.TopLeft = 0
  90. tview.Borders.TopRight = 0
  91. tview.Borders.BottomLeft = 0
  92. tview.Borders.BottomRight = 0
  93. tview.Borders.Horizontal = 0
  94. tview.Borders.Vertical = 0
  95. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(app.Config.Theme.Background)
  96. tview.Styles.BorderColor = tcell.GetColor(app.Config.Theme.Border)
  97. tview.Styles.TitleColor = tcell.GetColor(app.Config.Theme.Title)
  98. err := app.Run()
  99. if err != nil {
  100. panic(err)
  101. }
  102. }