main.go 3.6 KB

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