messages.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package ui
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "regexp"
  10. "strings"
  11. "github.com/atotto/clipboard"
  12. "github.com/ayntgl/astatine"
  13. "github.com/ayntgl/discordo/discord"
  14. "github.com/gdamore/tcell/v2"
  15. "github.com/rivo/tview"
  16. "github.com/skratchdot/open-golang/open"
  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.SetTitleAlign(tview.AlignLeft)
  35. mtv.SetBorder(true)
  36. mtv.SetBorderPadding(0, 0, 1, 1)
  37. mtv.SetInputCapture(mtv.onInputCapture)
  38. return mtv
  39. }
  40. func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  41. if mtv.app.SelectedChannel == nil {
  42. return nil
  43. }
  44. ms := mtv.app.SelectedChannel.Messages
  45. if len(ms) == 0 {
  46. return nil
  47. }
  48. switch e.Name() {
  49. case mtv.app.Config.Keys.SelectPreviousMessage:
  50. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  51. mtv.app.SelectedMessage = len(ms) - 1
  52. } else {
  53. mtv.app.SelectedMessage--
  54. if mtv.app.SelectedMessage < 0 {
  55. mtv.app.SelectedMessage = 0
  56. }
  57. }
  58. mtv.app.MessagesTextView.
  59. Highlight(ms[mtv.app.SelectedMessage].ID).
  60. ScrollToHighlight()
  61. return nil
  62. case mtv.app.Config.Keys.SelectNextMessage:
  63. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  64. mtv.app.SelectedMessage = len(ms) - 1
  65. } else {
  66. mtv.app.SelectedMessage++
  67. if mtv.app.SelectedMessage >= len(ms) {
  68. mtv.app.SelectedMessage = len(ms) - 1
  69. }
  70. }
  71. mtv.app.MessagesTextView.
  72. Highlight(ms[mtv.app.SelectedMessage].ID).
  73. ScrollToHighlight()
  74. return nil
  75. case mtv.app.Config.Keys.SelectFirstMessage:
  76. mtv.app.SelectedMessage = 0
  77. mtv.app.MessagesTextView.
  78. Highlight(ms[mtv.app.SelectedMessage].ID).
  79. ScrollToHighlight()
  80. return nil
  81. case mtv.app.Config.Keys.SelectLastMessage:
  82. mtv.app.SelectedMessage = len(ms) - 1
  83. mtv.app.MessagesTextView.
  84. Highlight(ms[mtv.app.SelectedMessage].ID).
  85. ScrollToHighlight()
  86. return nil
  87. case mtv.app.Config.Keys.OpenMessageActionsList:
  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. actionsList := tview.NewList()
  97. actionsList.ShowSecondaryText(false)
  98. actionsList.SetDoneFunc(func() {
  99. mtv.app.
  100. SetRoot(mtv.app.MainFlex, true).
  101. SetFocus(mtv.app.MessagesTextView)
  102. })
  103. actionsList.SetTitle("Press the Escape key to close")
  104. actionsList.SetTitleAlign(tview.AlignLeft)
  105. actionsList.SetBorder(true)
  106. actionsList.SetBorderPadding(0, 0, 1, 1)
  107. // If the client user has `SEND_MESSAGES` permission, add the appropriate actions to the list.
  108. if discord.HasPermission(mtv.app.Session.State, mtv.app.SelectedChannel.ID, astatine.PermissionSendMessages) {
  109. actionsList.AddItem("Reply", "", 'r', func() {
  110. mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  111. mtv.app.
  112. SetRoot(mtv.app.MainFlex, true).
  113. SetFocus(mtv.app.MessageInputField)
  114. })
  115. actionsList.AddItem("Mention Reply", "", 'R', func() {
  116. mtv.app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  117. mtv.app.
  118. SetRoot(mtv.app.MainFlex, true).
  119. SetFocus(mtv.app.MessageInputField)
  120. })
  121. }
  122. // If the referenced message exists, add the appropriate actions to the list.
  123. if m.ReferencedMessage != nil {
  124. actionsList.AddItem("Select Reply", "", 'm', func() {
  125. mtv.app.SelectedMessage, _ = discord.FindMessageByID(mtv.app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  126. mtv.app.MessagesTextView.
  127. Highlight(m.ReferencedMessage.ID).
  128. ScrollToHighlight()
  129. mtv.app.
  130. SetRoot(mtv.app.MainFlex, true).
  131. SetFocus(mtv.app.MessagesTextView)
  132. })
  133. }
  134. // If the content of the message contains link(s), add the appropriate actions to the list.
  135. links := linkRegex.FindAllString(m.Content, -1)
  136. if len(links) != 0 {
  137. actionsList.AddItem("Open Link", "", 'l', func() {
  138. for _, l := range links {
  139. go open.Run(l)
  140. }
  141. })
  142. }
  143. // If the message contains attachments, add the appropriate actions to the actions list.
  144. if len(m.Attachments) != 0 {
  145. actionsList.AddItem("Download Attachment", "", 'd', func() {
  146. go mtv.downloadAttachment(m.Attachments)
  147. mtv.app.SetRoot(mtv.app.MainFlex, true)
  148. })
  149. actionsList.AddItem("Open Attachment", "", 'o', func() {
  150. go mtv.openAttachment(m.Attachments)
  151. mtv.app.SetRoot(mtv.app.MainFlex, true)
  152. })
  153. }
  154. actionsList.AddItem("Copy Content", "", 'c', func() {
  155. if err := clipboard.WriteAll(m.Content); err != nil {
  156. return
  157. }
  158. mtv.app.SetRoot(mtv.app.MainFlex, true)
  159. mtv.app.SetFocus(mtv.app.MessagesTextView)
  160. })
  161. actionsList.AddItem("Copy ID", "", 'i', func() {
  162. if err := clipboard.WriteAll(m.ID); err != nil {
  163. return
  164. }
  165. mtv.app.SetRoot(mtv.app.MainFlex, true)
  166. mtv.app.SetFocus(mtv.app.MessagesTextView)
  167. })
  168. mtv.app.SetRoot(actionsList, true)
  169. return nil
  170. case "Esc":
  171. mtv.app.SelectedMessage = -1
  172. mtv.app.SetFocus(mtv.app.MainFlex)
  173. mtv.app.MessagesTextView.
  174. Clear().
  175. Highlight()
  176. return nil
  177. }
  178. return e
  179. }
  180. func (mtv *MessagesTextView) downloadAttachment(as []*astatine.MessageAttachment) error {
  181. for _, a := range as {
  182. f, err := os.Create(filepath.Join(mtv.app.Config.General.AttachmentDownloadsDir, a.Filename))
  183. if err != nil {
  184. return err
  185. }
  186. defer f.Close()
  187. resp, err := http.Get(a.URL)
  188. if err != nil {
  189. return err
  190. }
  191. d, err := ioutil.ReadAll(resp.Body)
  192. if err != nil {
  193. return err
  194. }
  195. f.Write(d)
  196. }
  197. return nil
  198. }
  199. func (mtv *MessagesTextView) openAttachment(as []*astatine.MessageAttachment) error {
  200. for _, a := range as {
  201. cacheDirPath, _ := os.UserCacheDir()
  202. f, err := os.Create(filepath.Join(cacheDirPath, a.Filename))
  203. if err != nil {
  204. return err
  205. }
  206. defer f.Close()
  207. resp, err := http.Get(a.URL)
  208. if err != nil {
  209. return err
  210. }
  211. d, err := ioutil.ReadAll(resp.Body)
  212. if err != nil {
  213. return err
  214. }
  215. f.Write(d)
  216. go open.Run(f.Name())
  217. }
  218. return nil
  219. }
  220. type MessageInputField struct {
  221. *tview.InputField
  222. app *App
  223. }
  224. func NewMessageInputField(app *App) *MessageInputField {
  225. mi := &MessageInputField{
  226. InputField: tview.NewInputField(),
  227. app: app,
  228. }
  229. mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
  230. mi.SetPlaceholder("Message...")
  231. mi.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor))
  232. mi.SetTitleAlign(tview.AlignLeft)
  233. mi.SetBorder(true)
  234. mi.SetBorderPadding(0, 0, 1, 1)
  235. mi.SetInputCapture(mi.onInputCapture)
  236. return mi
  237. }
  238. func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  239. switch e.Name() {
  240. case "Enter":
  241. if mi.app.SelectedChannel == nil {
  242. return nil
  243. }
  244. t := strings.TrimSpace(mi.app.MessageInputField.GetText())
  245. if t == "" {
  246. return nil
  247. }
  248. if len(mi.app.MessagesTextView.GetHighlights()) != 0 {
  249. _, m := discord.FindMessageByID(mi.app.SelectedChannel.Messages, mi.app.MessagesTextView.GetHighlights()[0])
  250. d := &astatine.MessageSend{
  251. Content: t,
  252. Reference: m.Reference(),
  253. AllowedMentions: &astatine.MessageAllowedMentions{RepliedUser: false},
  254. }
  255. if strings.HasPrefix(mi.app.MessageInputField.GetTitle(), "[@]") {
  256. d.AllowedMentions.RepliedUser = true
  257. } else {
  258. d.AllowedMentions.RepliedUser = false
  259. }
  260. go mi.app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  261. mi.app.SelectedMessage = -1
  262. mi.app.MessagesTextView.Highlight()
  263. mi.app.MessageInputField.SetTitle("")
  264. } else {
  265. go mi.app.Session.ChannelMessageSend(mi.app.SelectedChannel.ID, t)
  266. }
  267. mi.app.MessageInputField.SetText("")
  268. return nil
  269. case "Ctrl+V":
  270. text, _ := clipboard.ReadAll()
  271. text = mi.app.MessageInputField.GetText() + text
  272. mi.app.MessageInputField.SetText(text)
  273. return nil
  274. case "Esc":
  275. mi.app.MessageInputField.
  276. SetText("").
  277. SetTitle("")
  278. mi.app.SetFocus(mi.app.MainFlex)
  279. mi.app.SelectedMessage = -1
  280. mi.app.MessagesTextView.Highlight()
  281. return nil
  282. case mi.app.Config.Keys.OpenExternalEditor:
  283. e := os.Getenv("EDITOR")
  284. if e == "" {
  285. return nil
  286. }
  287. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  288. if err != nil {
  289. return nil
  290. }
  291. defer os.Remove(f.Name())
  292. cmd := exec.Command(e, f.Name())
  293. cmd.Stdin = os.Stdin
  294. cmd.Stdout = os.Stdout
  295. mi.app.Suspend(func() {
  296. err = cmd.Run()
  297. if err != nil {
  298. return
  299. }
  300. })
  301. b, err := io.ReadAll(f)
  302. if err != nil {
  303. return nil
  304. }
  305. mi.app.MessageInputField.SetText(string(b))
  306. return nil
  307. }
  308. return e
  309. }