main.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 = tview.NewTreeView()
  50. channelsTree.
  51. SetTopLevel(1).
  52. SetRoot(tview.NewTreeNode("")).
  53. SetSelectedFunc(onChannelsTreeSelected).
  54. SetTitle("Channels").
  55. SetTitleAlign(tview.AlignLeft).
  56. SetBorder(true).
  57. SetBorderPadding(0, 0, 1, 0)
  58. messagesView = tview.NewTextView()
  59. messagesView.
  60. SetRegions(true).
  61. SetDynamicColors(true).
  62. SetWordWrap(true).
  63. SetChangedFunc(func() { app.Draw() }).
  64. SetBorder(true).
  65. SetBorderPadding(0, 0, 1, 0).
  66. SetTitleAlign(tview.AlignLeft).
  67. SetInputCapture(onMessagesViewInputCapture)
  68. messageInputField = tview.NewInputField()
  69. messageInputField.
  70. SetPlaceholder("Message...").
  71. SetPlaceholderTextColor(tcell.ColorWhite).
  72. SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
  73. SetBorder(true).
  74. SetBorderPadding(0, 0, 1, 0).
  75. SetTitleAlign(tview.AlignLeft).
  76. SetInputCapture(onMessageInputFieldInputCapture)
  77. rightFlex := tview.NewFlex().
  78. SetDirection(tview.FlexRow).
  79. AddItem(messagesView, 0, 1, false).
  80. AddItem(messageInputField, 3, 1, false)
  81. mainFlex = tview.NewFlex().
  82. AddItem(channelsTree, 0, 1, false).
  83. AddItem(rightFlex, 0, 4, false)
  84. token, err := keyring.Get(service, "token")
  85. if err != nil {
  86. token = conf.Token
  87. }
  88. if token != "" {
  89. app.
  90. SetRoot(mainFlex, true).
  91. SetFocus(channelsTree)
  92. session = newSession()
  93. session.Token = token
  94. session.Identify.Token = token
  95. if err := session.Open(); err != nil {
  96. panic(err)
  97. }
  98. } else {
  99. loginForm = newLoginForm(onLoginFormLoginButtonSelected, false)
  100. app.SetRoot(loginForm, true)
  101. }
  102. if err := app.Run(); err != nil {
  103. panic(err)
  104. }
  105. }
  106. func onLoginFormLoginButtonSelected() {
  107. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  108. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  109. if email == "" || password == "" {
  110. return
  111. }
  112. session = newSession()
  113. // Login using the email and password
  114. lr, err := login(email, password)
  115. if err != nil {
  116. panic(err)
  117. }
  118. if lr.Token != "" && !lr.MFA {
  119. app.
  120. SetRoot(mainFlex, true).
  121. SetFocus(channelsTree)
  122. session.Token = lr.Token
  123. session.Identify.Token = lr.Token
  124. if err = session.Open(); err != nil {
  125. panic(err)
  126. }
  127. go keyring.Set(service, "token", lr.Token)
  128. } else if lr.MFA {
  129. // The account has MFA enabled, reattempt login with code and ticket.
  130. loginForm = newLoginForm(func() {
  131. code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  132. if code == "" {
  133. return
  134. }
  135. lr, err = totp(code, lr.Ticket)
  136. if err != nil {
  137. panic(err)
  138. }
  139. app.
  140. SetRoot(mainFlex, true).
  141. SetFocus(channelsTree)
  142. session.Token = lr.Token
  143. session.Identify.Token = lr.Token
  144. if err = session.Open(); err != nil {
  145. panic(err)
  146. }
  147. go keyring.Set(service, "token", lr.Token)
  148. }, true)
  149. app.SetRoot(loginForm, true)
  150. }
  151. }