handlers.go 9.0 KB

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