main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "github.com/ayntgl/discordgo"
  4. "github.com/gdamore/tcell/v2"
  5. "github.com/rivo/tview"
  6. "github.com/zalando/go-keyring"
  7. )
  8. const service = "discordo"
  9. var (
  10. app *tview.Application
  11. loginForm *tview.Form
  12. channelsTree *tview.TreeView
  13. messagesView *tview.TextView
  14. messageInputField *tview.InputField
  15. mainFlex *tview.Flex
  16. conf *config
  17. session *discordgo.Session
  18. )
  19. func main() {
  20. conf = loadConfig()
  21. tview.Borders = conf.Borders
  22. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(conf.Theme.Background.Primitive)
  23. tview.Styles.ContrastBackgroundColor = tcell.GetColor(conf.Theme.Background.Contrast)
  24. tview.Styles.MoreContrastBackgroundColor = tcell.GetColor(conf.Theme.Background.MoreContrast)
  25. tview.Styles.BorderColor = tcell.GetColor(conf.Theme.Border)
  26. tview.Styles.TitleColor = tcell.GetColor(conf.Theme.Title)
  27. tview.Styles.GraphicsColor = tcell.GetColor(conf.Theme.Graphics)
  28. tview.Styles.PrimaryTextColor = tcell.GetColor(conf.Theme.Text.Primary)
  29. tview.Styles.SecondaryTextColor = tcell.GetColor(conf.Theme.Text.Secondary)
  30. tview.Styles.TertiaryTextColor = tcell.GetColor(conf.Theme.Text.Tertiary)
  31. tview.Styles.InverseTextColor = tcell.GetColor(conf.Theme.Text.Inverse)
  32. tview.Styles.ContrastSecondaryTextColor = tcell.GetColor(conf.Theme.Text.ContrastSecondary)
  33. app = newApp()
  34. channelsTree = newChannelsTree()
  35. messagesView = newMessagesView()
  36. messageInputField = newMessageInputField()
  37. rightFlex := tview.NewFlex().
  38. SetDirection(tview.FlexRow).
  39. AddItem(messagesView, 0, 1, false).
  40. AddItem(messageInputField, 3, 1, false)
  41. mainFlex = tview.NewFlex().
  42. AddItem(channelsTree, 0, 1, false).
  43. AddItem(rightFlex, 0, 4, false)
  44. token, err := keyring.Get(service, "token")
  45. if err != nil {
  46. token = conf.Token
  47. }
  48. if token != "" {
  49. app.
  50. SetRoot(mainFlex, true).
  51. SetFocus(channelsTree)
  52. session = newSession()
  53. session.Token = token
  54. session.Identify.Token = token
  55. if err := session.Open(); err != nil {
  56. panic(err)
  57. }
  58. } else {
  59. loginForm = newLoginForm(onLoginFormLoginButtonSelected, false)
  60. app.SetRoot(loginForm, true)
  61. }
  62. if err := app.Run(); err != nil {
  63. panic(err)
  64. }
  65. }
  66. func onLoginFormLoginButtonSelected() {
  67. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  68. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  69. if email == "" || password == "" {
  70. return
  71. }
  72. session = newSession()
  73. // Login using the email and password
  74. lr, err := login(email, password)
  75. if err != nil {
  76. panic(err)
  77. }
  78. if lr.Token != "" && !lr.MFA {
  79. app.
  80. SetRoot(mainFlex, true).
  81. SetFocus(channelsTree)
  82. session.Token = lr.Token
  83. session.Identify.Token = lr.Token
  84. if err = session.Open(); err != nil {
  85. panic(err)
  86. }
  87. go keyring.Set(service, "token", lr.Token)
  88. } else if lr.MFA {
  89. // The account has MFA enabled, reattempt login with code and ticket.
  90. loginForm = newLoginForm(func() {
  91. code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  92. if code == "" {
  93. return
  94. }
  95. lr, err = totp(code, lr.Ticket)
  96. if err != nil {
  97. panic(err)
  98. }
  99. app.
  100. SetRoot(mainFlex, true).
  101. SetFocus(channelsTree)
  102. session.Token = lr.Token
  103. session.Identify.Token = lr.Token
  104. if err = session.Open(); err != nil {
  105. panic(err)
  106. }
  107. go keyring.Set(service, "token", lr.Token)
  108. }, true)
  109. app.SetRoot(loginForm, true)
  110. }
  111. }