2021-07-20 05:22:29 +03:00
|
|
|
package events
|
2020-05-24 03:57:49 +03:00
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
2021-03-09 10:20:15 +03:00
|
|
|
"regexp"
|
2020-10-14 02:45:52 +03:00
|
|
|
"strings"
|
2023-07-06 03:25:47 +03:00
|
|
|
"sync"
|
|
|
|
"text/template"
|
2020-10-14 02:45:52 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/teris-io/shortid"
|
2020-10-14 02:45:52 +03:00
|
|
|
"github.com/yuin/goldmark"
|
2023-07-06 03:25:47 +03:00
|
|
|
emoji "github.com/yuin/goldmark-emoji"
|
|
|
|
emojiAst "github.com/yuin/goldmark-emoji/ast"
|
|
|
|
emojiDef "github.com/yuin/goldmark-emoji/definition"
|
2020-10-14 02:45:52 +03:00
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
2023-07-06 03:25:47 +03:00
|
|
|
"github.com/yuin/goldmark/util"
|
2020-10-14 02:45:52 +03:00
|
|
|
"mvdan.cc/xurls"
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2023-07-06 03:25:47 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/core/user"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-14 02:45:52 +03:00
|
|
|
)
|
2020-07-12 05:08:47 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// EventPayload is a generic key/value map for sending out to chat clients.
|
|
|
|
type EventPayload map[string]interface{}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// OutboundEvent represents an event that is sent out to all listeners of the chat server.
|
2021-07-20 05:22:29 +03:00
|
|
|
type OutboundEvent interface {
|
|
|
|
GetBroadcastPayload() EventPayload
|
|
|
|
GetMessageType() EventType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is any kind of event. A type is required to be specified.
|
|
|
|
type Event struct {
|
2023-05-30 20:31:43 +03:00
|
|
|
Timestamp time.Time `json:"timestamp"`
|
2021-08-13 07:20:06 +03:00
|
|
|
Type EventType `json:"type,omitempty"`
|
2021-09-12 10:18:15 +03:00
|
|
|
ID string `json:"id"`
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// UserEvent is an event with an associated user.
|
2021-07-20 05:22:29 +03:00
|
|
|
type UserEvent struct {
|
|
|
|
User *user.User `json:"user"`
|
|
|
|
HiddenAt *time.Time `json:"hiddenAt,omitempty"`
|
2023-05-30 20:31:43 +03:00
|
|
|
ClientID uint `json:"clientId,omitempty"`
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessageEvent is an event that has a message body.
|
|
|
|
type MessageEvent struct {
|
|
|
|
OutboundEvent `json:"-"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
RawBody string `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// SystemActionEvent is an event that represents an action that took place, not a chat message.
|
2021-07-20 05:22:29 +03:00
|
|
|
type SystemActionEvent struct {
|
|
|
|
Event
|
|
|
|
MessageEvent
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// SetDefaults will set default properties of all inbound events.
|
|
|
|
func (e *Event) SetDefaults() {
|
2021-09-12 10:18:15 +03:00
|
|
|
e.ID = shortid.MustGenerate()
|
2021-07-20 05:22:29 +03:00
|
|
|
e.Timestamp = time.Now()
|
2020-10-14 02:45:52 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// SetDefaults will set default properties of all inbound events.
|
|
|
|
func (e *UserMessageEvent) SetDefaults() {
|
2021-09-12 10:18:15 +03:00
|
|
|
e.ID = shortid.MustGenerate()
|
2021-07-20 05:22:29 +03:00
|
|
|
e.Timestamp = time.Now()
|
|
|
|
e.RenderAndSanitizeMessageBody()
|
2021-02-19 10:05:52 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 03:25:47 +03:00
|
|
|
// implements the emojiDef.Emojis interface but uses case-insensitive search.
|
|
|
|
// the .children field isn't currently used, but could be used in a future
|
|
|
|
// implementation of say, emoji packs where a child represents a pack.
|
|
|
|
type emojis struct {
|
|
|
|
list []emojiDef.Emoji
|
|
|
|
names map[string]*emojiDef.Emoji
|
|
|
|
children []emojiDef.Emojis
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a new Emojis set.
|
|
|
|
func newEmojis(emotes ...emojiDef.Emoji) emojiDef.Emojis {
|
|
|
|
self := &emojis{
|
|
|
|
list: emotes,
|
|
|
|
names: map[string]*emojiDef.Emoji{},
|
|
|
|
children: []emojiDef.Emojis{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range self.list {
|
|
|
|
emoji := &self.list[i]
|
|
|
|
for _, s := range emoji.ShortNames {
|
|
|
|
self.names[s] = emoji
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *emojis) Get(shortName string) (*emojiDef.Emoji, bool) {
|
|
|
|
v, ok := self.names[strings.ToLower(shortName)]
|
|
|
|
if ok {
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range self.children {
|
|
|
|
v, ok := child.Get(shortName)
|
|
|
|
if ok {
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *emojis) Add(emotes emojiDef.Emojis) {
|
|
|
|
self.children = append(self.children, emotes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *emojis) Clone() emojiDef.Emojis {
|
|
|
|
clone := &emojis{
|
|
|
|
list: self.list,
|
|
|
|
names: self.names,
|
|
|
|
children: make([]emojiDef.Emojis, len(self.children)),
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(clone.children, self.children)
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
emojiMu sync.Mutex
|
|
|
|
emojiDefs = newEmojis()
|
|
|
|
emojiHTML = make(map[string]string)
|
|
|
|
emojiModTime time.Time
|
|
|
|
emojiHTMLFormat = `<img src="{{ .URL }}" class="emoji" alt=":{{ .Name }}:" title=":{{ .Name }}:">`
|
|
|
|
emojiHTMLTemplate = template.Must(template.New("emojiHTML").Parse(emojiHTMLFormat))
|
|
|
|
)
|
|
|
|
|
|
|
|
func loadEmoji() {
|
|
|
|
modTime, err := data.UpdateEmojiList(false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if modTime.After(emojiModTime) {
|
|
|
|
emojiMu.Lock()
|
|
|
|
defer emojiMu.Unlock()
|
|
|
|
|
|
|
|
emojiHTML = make(map[string]string)
|
|
|
|
|
|
|
|
emojiList := data.GetEmojiList()
|
|
|
|
emojiArr := make([]emojiDef.Emoji, 0)
|
|
|
|
|
|
|
|
for i := 0; i < len(emojiList); i++ {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := emojiHTMLTemplate.Execute(&buf, emojiList[i])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
emojiHTML[strings.ToLower(emojiList[i].Name)] = buf.String()
|
|
|
|
|
|
|
|
emoji := emojiDef.NewEmoji(emojiList[i].Name, nil, strings.ToLower(emojiList[i].Name))
|
|
|
|
emojiArr = append(emojiArr, emoji)
|
|
|
|
}
|
|
|
|
|
|
|
|
emojiDefs = newEmojis(emojiArr...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
// RenderAndSanitizeMessageBody will turn markdown into HTML, sanitize raw user-supplied HTML and standardize
|
|
|
|
// the message into something safe and renderable for clients.
|
2021-07-20 05:22:29 +03:00
|
|
|
func (m *MessageEvent) RenderAndSanitizeMessageBody() {
|
2021-02-19 10:05:52 +03:00
|
|
|
m.RawBody = m.Body
|
2020-10-14 02:45:52 +03:00
|
|
|
|
|
|
|
// Set the new, sanitized and rendered message body
|
2021-02-19 10:05:52 +03:00
|
|
|
m.Body = RenderAndSanitize(m.RawBody)
|
2020-10-14 02:45:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
// Empty will return if this message's contents is empty.
|
2021-07-20 05:22:29 +03:00
|
|
|
func (m *MessageEvent) Empty() bool {
|
2021-07-28 01:26:27 +03:00
|
|
|
return m.Body == ""
|
2021-02-01 01:57:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// RenderBody will render markdown to html without any sanitization.
|
|
|
|
func (m *MessageEvent) RenderBody() {
|
2021-03-12 11:43:10 +03:00
|
|
|
m.RawBody = m.Body
|
|
|
|
m.Body = RenderMarkdown(m.RawBody)
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
// RenderAndSanitize will turn markdown into HTML, sanitize raw user-supplied HTML and standardize
|
|
|
|
// the message into something safe and renderable for clients.
|
|
|
|
func RenderAndSanitize(raw string) string {
|
2021-03-12 11:43:10 +03:00
|
|
|
rendered := RenderMarkdown(raw)
|
2020-10-14 02:45:52 +03:00
|
|
|
safe := sanitize(rendered)
|
|
|
|
|
|
|
|
// Set the new, sanitized and rendered message body
|
|
|
|
return strings.TrimSpace(safe)
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// RenderMarkdown will return HTML rendered from the string body of a chat message.
|
2021-03-12 11:43:10 +03:00
|
|
|
func RenderMarkdown(raw string) string {
|
2023-07-06 03:25:47 +03:00
|
|
|
loadEmoji()
|
|
|
|
|
|
|
|
emojiMu.Lock()
|
|
|
|
defer emojiMu.Unlock()
|
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
markdown := goldmark.New(
|
|
|
|
goldmark.WithRendererOptions(
|
2021-08-19 03:00:20 +03:00
|
|
|
html.WithUnsafe(),
|
2020-10-14 02:45:52 +03:00
|
|
|
),
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.NewLinkify(
|
|
|
|
extension.WithLinkifyAllowedProtocols([][]byte{
|
|
|
|
[]byte("http:"),
|
|
|
|
[]byte("https:"),
|
|
|
|
}),
|
|
|
|
extension.WithLinkifyURLRegexp(
|
|
|
|
xurls.Strict,
|
|
|
|
),
|
|
|
|
),
|
2023-07-06 03:25:47 +03:00
|
|
|
emoji.New(
|
|
|
|
emoji.WithEmojis(
|
|
|
|
emojiDefs,
|
|
|
|
),
|
|
|
|
emoji.WithRenderingMethod(emoji.Func),
|
|
|
|
emoji.WithRendererFunc(func(w util.BufWriter, source []byte, n *emojiAst.Emoji, config *emoji.RendererConfig) {
|
|
|
|
baseName := n.Value.ShortNames[0]
|
|
|
|
_, _ = w.WriteString(emojiHTML[baseName])
|
|
|
|
}),
|
|
|
|
),
|
2020-10-14 02:45:52 +03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
trimmed := strings.TrimSpace(raw)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := markdown.Convert([]byte(trimmed), &buf); err != nil {
|
2021-07-20 05:22:29 +03:00
|
|
|
log.Debugln(err)
|
2020-10-14 02:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2021-10-29 22:29:01 +03:00
|
|
|
var (
|
2023-06-26 21:37:34 +03:00
|
|
|
_sanitizeReSrcMatch = regexp.MustCompile(`(?i)^/img/emoji/[^\.%]*.[A-Z]*$`)
|
|
|
|
_sanitizeReClassMatch = regexp.MustCompile(`(?i)^(emoji)[A-Z_]*?$`)
|
|
|
|
_sanitizeNonEmptyMatch = regexp.MustCompile(`^.+$`)
|
2021-10-29 22:29:01 +03:00
|
|
|
)
|
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
func sanitize(raw string) string {
|
|
|
|
p := bluemonday.StrictPolicy()
|
|
|
|
|
|
|
|
// Require URLs to be parseable by net/url.Parse
|
|
|
|
p.AllowStandardURLs()
|
2021-03-09 10:20:15 +03:00
|
|
|
p.RequireParseableURLs(true)
|
2020-10-14 02:45:52 +03:00
|
|
|
|
|
|
|
// Allow links
|
|
|
|
p.AllowAttrs("href").OnElements("a")
|
|
|
|
|
|
|
|
// Force all URLs to have "noreferrer" in their rel attribute.
|
|
|
|
p.RequireNoReferrerOnLinks(true)
|
|
|
|
|
|
|
|
// Links will get target="_blank" added to them.
|
|
|
|
p.AddTargetBlankToFullyQualifiedLinks(true)
|
|
|
|
|
2021-03-09 10:20:15 +03:00
|
|
|
// Allow breaks
|
2021-07-28 01:26:27 +03:00
|
|
|
p.AllowElements("br")
|
|
|
|
|
2023-06-28 00:45:45 +03:00
|
|
|
p.AllowElements("p")
|
2020-10-14 02:45:52 +03:00
|
|
|
|
2021-03-09 10:20:15 +03:00
|
|
|
// Allow img tags from the the local emoji directory only
|
2021-10-29 22:29:01 +03:00
|
|
|
p.AllowAttrs("src").Matching(_sanitizeReSrcMatch).OnElements("img")
|
2023-06-26 21:37:34 +03:00
|
|
|
p.AllowAttrs("alt", "title").Matching(_sanitizeNonEmptyMatch).OnElements("img")
|
2022-03-09 00:03:37 +03:00
|
|
|
p.AllowAttrs("class").Matching(_sanitizeReClassMatch).OnElements("img")
|
2020-10-14 02:45:52 +03:00
|
|
|
|
|
|
|
// Allow bold
|
|
|
|
p.AllowElements("strong")
|
|
|
|
|
|
|
|
// Allow emphasis
|
|
|
|
p.AllowElements("em")
|
|
|
|
|
2021-02-01 01:57:50 +03:00
|
|
|
// Allow code blocks
|
|
|
|
p.AllowElements("code")
|
|
|
|
p.AllowElements("pre")
|
|
|
|
|
2020-10-14 02:45:52 +03:00
|
|
|
return p.Sanitize(raw)
|
2020-06-23 23:11:01 +03:00
|
|
|
}
|