main.go 3.5 KB

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