handlers.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package ui
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/atotto/clipboard"
  6. "github.com/ayntgl/discordgo"
  7. util "github.com/ayntgl/discordo/discord"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rivo/tview"
  10. "github.com/zalando/go-keyring"
  11. )
  12. func onAppInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  13. if hasKeybinding(app.Config.Keybindings.FocusChannelsTreeView, e.Name()) {
  14. app.SetFocus(app.ChannelsTreeView)
  15. return nil
  16. } else if hasKeybinding(app.Config.Keybindings.FocusMessagesTextView, e.Name()) {
  17. app.SetFocus(app.MessagesTextView)
  18. return nil
  19. } else if hasKeybinding(app.Config.Keybindings.FocusMessageInputField, e.Name()) {
  20. app.SetFocus(app.MessageInputField)
  21. return nil
  22. }
  23. return e
  24. }
  25. func onGuildsListSelected(app *App, guildIdx int) {
  26. rootTreeNode := app.ChannelsTreeView.GetRoot()
  27. rootTreeNode.ClearChildren()
  28. app.MessagesTextView.
  29. Highlight().
  30. Clear().
  31. SetTitle("")
  32. app.MessageInputField.SetText("")
  33. if guildIdx == 0 { // Direct Messages
  34. cs := app.Session.State.PrivateChannels
  35. sort.Slice(cs, func(i, j int) bool {
  36. return cs[i].LastMessageID > cs[j].LastMessageID
  37. })
  38. for _, c := range cs {
  39. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  40. SetReference(c.ID)
  41. rootTreeNode.AddChild(channelTreeNode)
  42. }
  43. } else { // Guild
  44. cs := app.Session.State.Guilds[guildIdx-1].Channels
  45. sort.Slice(cs, func(i, j int) bool {
  46. return cs[i].Position < cs[j].Position
  47. })
  48. for _, c := range cs {
  49. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
  50. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  51. SetReference(c.ID)
  52. rootTreeNode.AddChild(channelTreeNode)
  53. }
  54. }
  55. CATEGORY:
  56. for _, c := range cs {
  57. if c.Type == discordgo.ChannelTypeGuildCategory {
  58. for _, nestedChannel := range cs {
  59. if nestedChannel.ParentID == c.ID {
  60. channelTreeNode := tview.NewTreeNode(c.Name).
  61. SetReference(c.ID)
  62. rootTreeNode.AddChild(channelTreeNode)
  63. continue CATEGORY
  64. }
  65. }
  66. channelTreeNode := tview.NewTreeNode(c.Name).
  67. SetReference(c.ID)
  68. rootTreeNode.AddChild(channelTreeNode)
  69. }
  70. }
  71. for _, c := range cs {
  72. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
  73. var parentTreeNode *tview.TreeNode
  74. rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
  75. if node.GetReference() == c.ParentID {
  76. parentTreeNode = node
  77. return false
  78. }
  79. return true
  80. })
  81. if parentTreeNode != nil {
  82. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  83. SetReference(c.ID)
  84. parentTreeNode.AddChild(channelTreeNode)
  85. }
  86. }
  87. }
  88. }
  89. app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
  90. app.SetFocus(app.ChannelsTreeView)
  91. }
  92. func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
  93. c, err := app.Session.State.Channel(n.GetReference().(string))
  94. if err != nil {
  95. return
  96. }
  97. if c.Type == discordgo.ChannelTypeGuildCategory {
  98. n.SetExpanded(!n.IsExpanded())
  99. return
  100. }
  101. app.SelectedChannel = c
  102. app.MessagesTextView.SetTitle(channelToString(c))
  103. app.SetFocus(app.MessageInputField)
  104. go func() {
  105. ms, err := app.Session.ChannelMessages(c.ID, app.Config.General.FetchMessagesLimit, "", "", "")
  106. if err != nil {
  107. return
  108. }
  109. for i := len(ms) - 1; i >= 0; i-- {
  110. app.SelectedChannel.Messages = ms
  111. app.MessagesTextView.Write(buildMessage(app, ms[i]))
  112. }
  113. app.MessagesTextView.ScrollToEnd()
  114. }()
  115. }
  116. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  117. if app.SelectedChannel == nil {
  118. return nil
  119. }
  120. ms := app.SelectedChannel.Messages
  121. if len(ms) == 0 {
  122. return nil
  123. }
  124. if hasKeybinding(app.Config.Keybindings.SelectPreviousMessage, e.Name()) {
  125. if len(app.MessagesTextView.GetHighlights()) == 0 {
  126. app.SelectedMessage = len(ms) - 1
  127. } else {
  128. app.SelectedMessage--
  129. if app.SelectedMessage < 0 {
  130. app.SelectedMessage = 0
  131. }
  132. }
  133. app.MessagesTextView.
  134. Highlight(ms[app.SelectedMessage].ID).
  135. ScrollToHighlight()
  136. return nil
  137. } else if hasKeybinding(app.Config.Keybindings.SelectNextMessage, e.Name()) {
  138. if len(app.MessagesTextView.GetHighlights()) == 0 {
  139. app.SelectedMessage = len(ms) - 1
  140. } else {
  141. app.SelectedMessage++
  142. if app.SelectedMessage >= len(ms) {
  143. app.SelectedMessage = len(ms) - 1
  144. }
  145. }
  146. app.MessagesTextView.
  147. Highlight(ms[app.SelectedMessage].ID).
  148. ScrollToHighlight()
  149. return nil
  150. } else if hasKeybinding(app.Config.Keybindings.SelectFirstMessage, e.Name()) {
  151. app.SelectedMessage = 0
  152. app.MessagesTextView.
  153. Highlight(ms[app.SelectedMessage].ID).
  154. ScrollToHighlight()
  155. return nil
  156. } else if hasKeybinding(app.Config.Keybindings.SelectLastMessage, e.Name()) {
  157. app.SelectedMessage = len(ms) - 1
  158. app.MessagesTextView.
  159. Highlight(ms[app.SelectedMessage].ID).
  160. ScrollToHighlight()
  161. return nil
  162. } else if hasKeybinding(app.Config.Keybindings.SelectMessageReference, e.Name()) {
  163. hs := app.MessagesTextView.GetHighlights()
  164. if len(hs) == 0 {
  165. return nil
  166. }
  167. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  168. if m.ReferencedMessage != nil {
  169. app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  170. app.MessagesTextView.
  171. Highlight(m.ReferencedMessage.ID).
  172. ScrollToHighlight()
  173. }
  174. return nil
  175. } else if hasKeybinding(app.Config.Keybindings.ReplySelectedMessage, e.Name()) {
  176. hs := app.MessagesTextView.GetHighlights()
  177. if len(hs) == 0 {
  178. return nil
  179. }
  180. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  181. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  182. app.SetFocus(app.MessageInputField)
  183. return nil
  184. } else if hasKeybinding(app.Config.Keybindings.MentionReplySelectedMessage, e.Name()) {
  185. hs := app.MessagesTextView.GetHighlights()
  186. if len(hs) == 0 {
  187. return nil
  188. }
  189. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  190. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  191. app.SetFocus(app.MessageInputField)
  192. return nil
  193. } else if hasKeybinding(app.Config.Keybindings.CopySelectedMessage, e.Name()) {
  194. hs := app.MessagesTextView.GetHighlights()
  195. if len(hs) == 0 {
  196. return nil
  197. }
  198. _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  199. err := clipboard.WriteAll(m.Content)
  200. if err != nil {
  201. return nil
  202. }
  203. return nil
  204. }
  205. return e
  206. }
  207. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  208. switch e.Key() {
  209. case tcell.KeyEnter:
  210. if app.SelectedChannel == nil {
  211. return nil
  212. }
  213. t := strings.TrimSpace(app.MessageInputField.GetText())
  214. if t == "" {
  215. return nil
  216. }
  217. if len(app.MessagesTextView.GetHighlights()) != 0 {
  218. _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  219. d := &discordgo.MessageSend{
  220. Content: t,
  221. Reference: m.Reference(),
  222. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  223. }
  224. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  225. d.AllowedMentions.RepliedUser = true
  226. } else {
  227. d.AllowedMentions.RepliedUser = false
  228. }
  229. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  230. app.SelectedMessage = -1
  231. app.MessagesTextView.Highlight()
  232. app.MessageInputField.SetTitle("")
  233. } else {
  234. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  235. }
  236. app.MessageInputField.SetText("")
  237. return nil
  238. case tcell.KeyCtrlV:
  239. text, _ := clipboard.ReadAll()
  240. text = app.MessageInputField.GetText() + text
  241. app.MessageInputField.SetText(text)
  242. return nil
  243. case tcell.KeyEscape:
  244. app.MessageInputField.SetText("")
  245. app.MessageInputField.SetTitle("")
  246. app.SelectedMessage = -1
  247. app.MessagesTextView.Highlight()
  248. return nil
  249. }
  250. return e
  251. }
  252. func OnLoginFormLoginButtonSelected(app *App) {
  253. email := app.LoginForm.GetFormItem(0).(*tview.InputField).GetText()
  254. password := app.LoginForm.GetFormItem(1).(*tview.InputField).GetText()
  255. if email == "" || password == "" {
  256. return
  257. }
  258. // Login using the email and password
  259. lr, err := util.Login(app.Session, email, password)
  260. if err != nil {
  261. panic(err)
  262. }
  263. if lr.Token != "" && !lr.MFA {
  264. app.
  265. SetRoot(NewMainFlex(app), true).
  266. SetFocus(app.GuildsList)
  267. err = app.Connect(lr.Token)
  268. if err != nil {
  269. panic(err)
  270. }
  271. go keyring.Set("discordo", "token", lr.Token)
  272. } else if lr.MFA {
  273. // The account has MFA enabled, reattempt login with code and ticket.
  274. NewLoginForm(app, func() {
  275. code := app.LoginForm.GetFormItem(0).(*tview.InputField).GetText()
  276. if code == "" {
  277. return
  278. }
  279. lr, err = util.TOTP(app.Session, code, lr.Ticket)
  280. if err != nil {
  281. panic(err)
  282. }
  283. app.
  284. SetRoot(NewMainFlex(app), true).
  285. SetFocus(app.GuildsList)
  286. err = app.Connect(lr.Token)
  287. if err != nil {
  288. panic(err)
  289. }
  290. go keyring.Set("discordo", "token", lr.Token)
  291. }, true)
  292. app.SetRoot(app.LoginForm, true)
  293. }
  294. }