main.go 4.2 KB

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