main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "github.com/ayntgl/discordo/ui"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rivo/tview"
  10. lua "github.com/yuin/gopher-lua"
  11. "github.com/zalando/go-keyring"
  12. )
  13. const (
  14. name = "discordo"
  15. )
  16. var (
  17. token string
  18. configPath string
  19. )
  20. func init() {
  21. flag.StringVar(&token, "token", "", "The client authentication token.")
  22. // If the token is provided via a command-line flag, store it in the default keyring.
  23. if token != "" {
  24. go keyring.Set(name, "token", token)
  25. }
  26. if token == "" {
  27. token, _ = keyring.Get(name, "token")
  28. }
  29. configDirPath, err := os.UserConfigDir()
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. flag.StringVar(&configPath, "config", filepath.Join(configDirPath, name), "The path to the configuration directory.")
  34. }
  35. func main() {
  36. flag.Parse()
  37. c := ui.NewCore(configPath)
  38. if token != "" {
  39. err := c.Run(token)
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. c.DrawMainFlex()
  44. c.Application.SetRoot(c.MainFlex, true)
  45. c.Application.SetFocus(c.GuildsTree)
  46. } else {
  47. loginForm := ui.NewLoginForm(false)
  48. loginForm.AddButton("Login", func() {
  49. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  50. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  51. if email == "" || password == "" {
  52. return
  53. }
  54. // Login using the email and password only
  55. lr, err := c.State.Login(email, password)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. if lr.Token != "" && !lr.MFA {
  60. err = c.Run(lr.Token)
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. c.DrawMainFlex()
  65. c.Application.SetRoot(c.MainFlex, true)
  66. c.Application.SetFocus(c.GuildsTree)
  67. go keyring.Set(name, "token", lr.Token)
  68. } else {
  69. // The account has MFA enabled, reattempt login with MFA code and ticket.
  70. mfaLoginForm := ui.NewLoginForm(true)
  71. mfaLoginForm.AddButton("Login", func() {
  72. code := mfaLoginForm.GetFormItem(0).(*tview.InputField).GetText()
  73. if code == "" {
  74. return
  75. }
  76. lr, err = c.State.TOTP(code, lr.Ticket)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. err = c.Run(lr.Token)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. c.DrawMainFlex()
  85. c.Application.SetRoot(c.MainFlex, true)
  86. c.Application.SetFocus(c.GuildsTree)
  87. go keyring.Set(name, "token", lr.Token)
  88. })
  89. c.Application.SetRoot(mfaLoginForm, true)
  90. }
  91. })
  92. c.Application.SetRoot(loginForm, true)
  93. }
  94. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  95. tview.Borders.TopRightFocus = tview.Borders.TopRight
  96. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  97. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  98. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  99. tview.Borders.VerticalFocus = tview.Borders.Vertical
  100. tview.Borders.TopLeft = 0
  101. tview.Borders.TopRight = 0
  102. tview.Borders.BottomLeft = 0
  103. tview.Borders.BottomRight = 0
  104. tview.Borders.Horizontal = 0
  105. tview.Borders.Vertical = 0
  106. err := c.Config.Load()
  107. if err != nil {
  108. return
  109. }
  110. themeTable, ok := c.Config.State.GetGlobal("theme").(*lua.LTable)
  111. if !ok {
  112. themeTable = c.Config.State.NewTable()
  113. }
  114. background := themeTable.RawGetString("background")
  115. border := themeTable.RawGetString("border")
  116. title := themeTable.RawGetString("title")
  117. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(lua.LVAsString(background))
  118. tview.Styles.BorderColor = tcell.GetColor(lua.LVAsString(border))
  119. tview.Styles.TitleColor = tcell.GetColor(lua.LVAsString(title))
  120. err = c.Application.Run()
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. }