messages.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package ui
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "regexp"
  10. "runtime"
  11. "strings"
  12. "github.com/atotto/clipboard"
  13. "github.com/ayntgl/discordgo"
  14. "github.com/ayntgl/discordo/discord"
  15. "github.com/gdamore/tcell/v2"
  16. "github.com/rivo/tview"
  17. )
  18. var linkRegex = regexp.MustCompile("https?://.+")
  19. type MessagesTextView struct {
  20. *tview.TextView
  21. app *App
  22. }
  23. func NewMessagesTextView(app *App) *MessagesTextView {
  24. mtv := &MessagesTextView{
  25. TextView: tview.NewTextView(),
  26. app: app,
  27. }
  28. mtv.SetDynamicColors(true)
  29. mtv.SetRegions(true)
  30. mtv.SetWordWrap(true)
  31. mtv.SetChangedFunc(func() {
  32. mtv.app.Draw()
  33. })
  34. mtv.SetBorder(true)
  35. mtv.SetBorderPadding(0, 0, 1, 1)
  36. mtv.SetInputCapture(mtv.onInputCapture)
  37. return mtv
  38. }
  39. func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  40. if mtv.app.SelectedChannel == nil {
  41. return nil
  42. }
  43. ms := mtv.app.SelectedChannel.Messages
  44. if len(ms) == 0 {
  45. return nil
  46. }
  47. switch e.Name() {
  48. case mtv.app.Config.Keybindings.SelectPreviousMessage:
  49. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  50. mtv.app.SelectedMessage = len(ms) - 1
  51. } else {
  52. mtv.app.SelectedMessage--
  53. if mtv.app.SelectedMessage < 0 {
  54. mtv.app.SelectedMessage = 0
  55. }
  56. }
  57. mtv.app.MessagesTextView.
  58. Highlight(ms[mtv.app.SelectedMessage].ID).
  59. ScrollToHighlight()
  60. return nil
  61. case mtv.app.Config.Keybindings.SelectNextMessage:
  62. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  63. mtv.app.SelectedMessage = len(ms) - 1
  64. } else {
  65. mtv.app.SelectedMessage++
  66. if mtv.app.SelectedMessage >= len(ms) {
  67. mtv.app.SelectedMessage = len(ms) - 1
  68. }
  69. }
  70. mtv.app.MessagesTextView.
  71. Highlight(ms[mtv.app.SelectedMessage].ID).
  72. ScrollToHighlight()
  73. return nil
  74. case mtv.app.Config.Keybindings.SelectFirstMessage:
  75. mtv.app.SelectedMessage = 0
  76. mtv.app.MessagesTextView.
  77. Highlight(ms[mtv.app.SelectedMessage].ID).
  78. ScrollToHighlight()
  79. return nil
  80. case mtv.app.Config.Keybindings.SelectLastMessage:
  81. mtv.app.SelectedMessage = len(ms) - 1
  82. mtv.app.MessagesTextView.
  83. Highlight(ms[mtv.app.SelectedMessage].ID).
  84. ScrollToHighlight()
  85. return nil
  86. case mtv.app.Config.Keybindings.OpenMessageActionsList:
  87. messageActionsList := tview.NewList()
  88. hs := mtv.app.MessagesTextView.GetHighlights()
  89. if len(hs) == 0 {
  90. return nil
  91. }
  92. _, m := discord.FindMessageByID(mtv.app.SelectedChannel.Messages, hs[0])
  93. if m == nil {
  94. return nil
  95. }
  96. if discord.HasPermission(mtv.app.Session.State, mtv.app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
  97. messageActionsList.AddItem("Reply", "", 'r', func() {
  98. mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  99. mtv.app.
  100. SetRoot(mtv.app.MainFlex, true).
  101. SetFocus(mtv.app.MessageInputField)
  102. })
  103. messageActionsList.AddItem("Mention Reply", "", 'R', func() {
  104. mtv.app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  105. mtv.app.
  106. SetRoot(mtv.app.MainFlex, false).
  107. SetFocus(mtv.app.MessageInputField)
  108. })
  109. }
  110. if m.ReferencedMessage != nil {
  111. messageActionsList.AddItem("Select Reply", "", 'm', nil)
  112. }
  113. links := linkRegex.FindAllString(m.Content, -1)
  114. if len(links) != 0 {
  115. messageActionsList.AddItem("Open Link", "", 'l', func() {
  116. for _, l := range links {
  117. go Open(l)
  118. }
  119. })
  120. }
  121. if len(m.Attachments) != 0 {
  122. messageActionsList.AddItem("Download Attachment", "", 'd', nil)
  123. messageActionsList.AddItem("Open Attachment", "", 'o', nil)
  124. }
  125. messageActionsList.AddItem("Copy Content", "", 'c', func() {
  126. if err := clipboard.WriteAll(m.Content); err != nil {
  127. return
  128. }
  129. mtv.app.SetRoot(mtv.app.MainFlex, false)
  130. })
  131. messageActionsList.AddItem("Copy ID", "", 'i', func() {
  132. if err := clipboard.WriteAll(m.ID); err != nil {
  133. return
  134. }
  135. mtv.app.SetRoot(mtv.app.MainFlex, true)
  136. })
  137. messageActionsList.
  138. ShowSecondaryText(false).
  139. AddItem("Copy ID", "", 'i', nil).
  140. SetDoneFunc(func() {
  141. mtv.app.
  142. SetRoot(mtv.app.MainFlex, true).
  143. SetFocus(mtv.app.MessagesTextView)
  144. }).
  145. SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) {
  146. onMessageActionsListSelected(mtv.app, mainText, m)
  147. }).
  148. SetTitle("Press the Escape key to close").
  149. SetBorder(true)
  150. mtv.app.SetRoot(messageActionsList, true)
  151. return nil
  152. case "Esc":
  153. mtv.app.SelectedMessage = -1
  154. mtv.app.SetFocus(mtv.app.MainFlex)
  155. mtv.app.MessagesTextView.
  156. Clear().
  157. Highlight()
  158. return nil
  159. }
  160. return e
  161. }
  162. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  163. switch mainText {
  164. case "Select Reply":
  165. app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  166. app.MessagesTextView.
  167. Highlight(m.ReferencedMessage.ID).
  168. ScrollToHighlight()
  169. app.
  170. SetRoot(app.MainFlex, false).
  171. SetFocus(app.MessagesTextView)
  172. case "Download Attachment":
  173. for _, a := range m.Attachments {
  174. f, err := os.Create(filepath.Join(app.Config.General.AttachmentDownloadsDir, a.Filename))
  175. if err != nil {
  176. f.Close()
  177. return
  178. }
  179. defer f.Close()
  180. resp, err := http.Get(a.URL)
  181. if err != nil {
  182. return
  183. }
  184. d, err := ioutil.ReadAll(resp.Body)
  185. if err != nil {
  186. return
  187. }
  188. f.Write(d)
  189. }
  190. app.SetRoot(app.MainFlex, false)
  191. case "Open Attachment":
  192. for _, a := range m.Attachments {
  193. // We are caching the files, but files with the same name can still exist, so it ultimately does not matter
  194. cachePath, _ := os.UserCacheDir()
  195. f, err := os.Create(filepath.Join(cachePath, a.Filename))
  196. if err != nil {
  197. return
  198. }
  199. defer f.Close()
  200. resp, err := http.Get(a.URL)
  201. if err != nil {
  202. return
  203. }
  204. d, err := ioutil.ReadAll(resp.Body)
  205. if err != nil {
  206. return
  207. }
  208. f.Write(d)
  209. go Open(f.Name())
  210. }
  211. app.SetRoot(app.MainFlex, false)
  212. }
  213. }
  214. func Open(input string) {
  215. switch runtime.GOOS {
  216. case "windows":
  217. cmd := exec.Command("cmd", "/C start "+input)
  218. err := cmd.Run()
  219. if err != nil {
  220. return
  221. }
  222. case "darwin":
  223. cmd := exec.Command("open", input)
  224. err := cmd.Run()
  225. if err != nil {
  226. return
  227. }
  228. default: // Unix
  229. cmd := exec.Command("xdg-open", input)
  230. err := cmd.Run()
  231. if err != nil {
  232. return
  233. }
  234. }
  235. }
  236. type MessageInputField struct {
  237. *tview.InputField
  238. app *App
  239. }
  240. func NewMessageInputField(app *App) *MessageInputField {
  241. mi := &MessageInputField{
  242. InputField: tview.NewInputField(),
  243. app: app,
  244. }
  245. mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
  246. mi.SetPlaceholder("Message...")
  247. mi.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor))
  248. mi.SetTitleAlign(tview.AlignLeft)
  249. mi.SetBorder(true)
  250. mi.SetBorderPadding(0, 0, 1, 1)
  251. mi.SetInputCapture(mi.onInputCapture)
  252. return mi
  253. }
  254. func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  255. switch e.Name() {
  256. case "Enter":
  257. if mi.app.SelectedChannel == nil {
  258. return nil
  259. }
  260. t := strings.TrimSpace(mi.app.MessageInputField.GetText())
  261. if t == "" {
  262. return nil
  263. }
  264. if len(mi.app.MessagesTextView.GetHighlights()) != 0 {
  265. _, m := discord.FindMessageByID(mi.app.SelectedChannel.Messages, mi.app.MessagesTextView.GetHighlights()[0])
  266. d := &discordgo.MessageSend{
  267. Content: t,
  268. Reference: m.Reference(),
  269. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  270. }
  271. if strings.HasPrefix(mi.app.MessageInputField.GetTitle(), "[@]") {
  272. d.AllowedMentions.RepliedUser = true
  273. } else {
  274. d.AllowedMentions.RepliedUser = false
  275. }
  276. go mi.app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  277. mi.app.SelectedMessage = -1
  278. mi.app.MessagesTextView.Highlight()
  279. mi.app.MessageInputField.SetTitle("")
  280. } else {
  281. go mi.app.Session.ChannelMessageSend(mi.app.SelectedChannel.ID, t)
  282. }
  283. mi.app.MessageInputField.SetText("")
  284. return nil
  285. case "Ctrl+V":
  286. text, _ := clipboard.ReadAll()
  287. text = mi.app.MessageInputField.GetText() + text
  288. mi.app.MessageInputField.SetText(text)
  289. return nil
  290. case "Esc":
  291. mi.app.MessageInputField.
  292. SetText("").
  293. SetTitle("")
  294. mi.app.SetFocus(mi.app.MainFlex)
  295. mi.app.SelectedMessage = -1
  296. mi.app.MessagesTextView.Highlight()
  297. return nil
  298. case mi.app.Config.Keybindings.OpenExternalEditor:
  299. e := os.Getenv("EDITOR")
  300. if e == "" {
  301. return nil
  302. }
  303. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  304. if err != nil {
  305. return nil
  306. }
  307. defer os.Remove(f.Name())
  308. cmd := exec.Command(e, f.Name())
  309. cmd.Stdin = os.Stdin
  310. cmd.Stdout = os.Stdout
  311. mi.app.Suspend(func() {
  312. err = cmd.Run()
  313. if err != nil {
  314. return
  315. }
  316. })
  317. b, err := io.ReadAll(f)
  318. if err != nil {
  319. return nil
  320. }
  321. mi.app.MessageInputField.SetText(string(b))
  322. return nil
  323. }
  324. return e
  325. }