main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "os"
  4. "github.com/ayntgl/discordgo"
  5. "github.com/ayntgl/discordo/config"
  6. "github.com/ayntgl/discordo/ui"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/rivo/tview"
  9. "github.com/zalando/go-keyring"
  10. )
  11. const service = "discordo"
  12. var (
  13. app *ui.App
  14. loginForm *tview.Form
  15. channelsTreeView *tview.TreeView
  16. messagesTextView *tview.TextView
  17. messageInputField *tview.InputField
  18. mainFlex *tview.Flex
  19. selectedChannel *discordgo.Channel
  20. selectedMessage int = -1
  21. )
  22. func main() {
  23. app = ui.NewApp()
  24. app.
  25. EnableMouse(config.General.Mouse).
  26. SetInputCapture(onAppInputCapture)
  27. app.ChannelsTreeView.
  28. SetTopLevel(1).
  29. SetRoot(tview.NewTreeNode("")).
  30. SetSelectedFunc(onChannelsTreeSelected).
  31. SetTitleAlign(tview.AlignLeft).
  32. SetBorder(true).
  33. SetBorderPadding(0, 0, 1, 0)
  34. app.MessagesTextView.
  35. SetRegions(true).
  36. SetDynamicColors(true).
  37. SetWordWrap(true).
  38. SetChangedFunc(func() { app.Draw() }).
  39. SetTitleAlign(tview.AlignLeft).
  40. SetInputCapture(onMessagesViewInputCapture).
  41. SetBorder(true).
  42. SetBorderPadding(0, 0, 1, 0)
  43. app.MessageInputField.
  44. SetPlaceholder("Message...").
  45. SetPlaceholderTextColor(tcell.ColorWhite).
  46. SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
  47. SetInputCapture(onMessageInputFieldInputCapture).
  48. SetTitleAlign(tview.AlignLeft).
  49. SetBorder(true).
  50. SetBorderPadding(0, 0, 1, 0)
  51. rightFlex := tview.NewFlex().
  52. SetDirection(tview.FlexRow).
  53. AddItem(messagesTextView, 0, 1, false).
  54. AddItem(messageInputField, 3, 1, false)
  55. mainFlex = tview.NewFlex().
  56. AddItem(channelsTreeView, 0, 1, false).
  57. AddItem(rightFlex, 0, 4, false)
  58. token := os.Getenv("DISCORDO_TOKEN")
  59. if token == "" {
  60. token, _ = keyring.Get(service, "token")
  61. }
  62. if token != "" {
  63. app.
  64. SetRoot(mainFlex, true).
  65. SetFocus(channelsTreeView)
  66. app.Session.AddHandlerOnce(onSessionReady)
  67. app.Session.AddHandler(onSessionMessageCreate)
  68. err := app.Connect(token)
  69. if err != nil {
  70. panic(err)
  71. }
  72. } else {
  73. loginForm = ui.NewLoginForm(onLoginFormLoginButtonSelected, false)
  74. app.SetRoot(loginForm, true)
  75. }
  76. if err := app.Run(); err != nil {
  77. panic(err)
  78. }
  79. }
  80. func onLoginFormLoginButtonSelected() {
  81. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  82. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  83. if email == "" || password == "" {
  84. return
  85. }
  86. // Login using the email and password
  87. lr, err := login(email, password)
  88. if err != nil {
  89. panic(err)
  90. }
  91. if lr.Token != "" && !lr.MFA {
  92. app.
  93. SetRoot(mainFlex, true).
  94. SetFocus(channelsTreeView)
  95. app.Session.AddHandlerOnce(onSessionReady)
  96. app.Session.AddHandler(onSessionMessageCreate)
  97. err = app.Connect(lr.Token)
  98. if err != nil {
  99. panic(err)
  100. }
  101. go keyring.Set(service, "token", lr.Token)
  102. } else if lr.MFA {
  103. // The account has MFA enabled, reattempt login with code and ticket.
  104. loginForm = ui.NewLoginForm(func() {
  105. code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  106. if code == "" {
  107. return
  108. }
  109. lr, err = totp(code, lr.Ticket)
  110. if err != nil {
  111. panic(err)
  112. }
  113. app.
  114. SetRoot(mainFlex, true).
  115. SetFocus(channelsTreeView)
  116. app.Session.AddHandlerOnce(onSessionReady)
  117. app.Session.AddHandler(onSessionMessageCreate)
  118. err = app.Connect(lr.Token)
  119. if err != nil {
  120. panic(err)
  121. }
  122. go keyring.Set(service, "token", lr.Token)
  123. }, true)
  124. app.SetRoot(loginForm, true)
  125. }
  126. }