2019-03-13 06:09:24 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-03-14 20:21:53 +01:00
|
|
|
"errors"
|
2019-03-15 07:19:16 +01:00
|
|
|
"fmt"
|
2019-03-19 22:03:34 +01:00
|
|
|
"strings"
|
2019-03-13 06:09:24 +01:00
|
|
|
)
|
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
type DataInterface interface {
|
|
|
|
HTML() string
|
2019-03-14 02:24:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
type ChatData struct {
|
2019-03-21 13:47:40 +01:00
|
|
|
Type DataType
|
|
|
|
Data DataInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ChatData) ToJSON() (ChatDataJSON, error) {
|
|
|
|
rawData, err := json.Marshal(c.Data)
|
|
|
|
return ChatDataJSON{
|
|
|
|
Type: c.Type,
|
|
|
|
Data: rawData,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatDataJSON struct {
|
2019-03-15 07:19:16 +01:00
|
|
|
Type DataType
|
|
|
|
Data json.RawMessage
|
2019-03-14 20:21:53 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func (c ChatDataJSON) ToData() (ChatData, error) {
|
|
|
|
data, err := c.GetData()
|
|
|
|
return ChatData{
|
|
|
|
Type: c.Type,
|
|
|
|
Data: data,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ChatDataJSON) GetData() (DataInterface, error) {
|
2019-03-14 20:21:53 +01:00
|
|
|
var data DataInterface
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch c.Type {
|
|
|
|
case DTInvalid:
|
|
|
|
return nil, errors.New("data type is invalid")
|
|
|
|
case DTChat:
|
|
|
|
d := DataMessage{}
|
|
|
|
err = json.Unmarshal(c.Data, &d)
|
|
|
|
data = d
|
|
|
|
case DTCommand:
|
|
|
|
d := DataCommand{}
|
|
|
|
err = json.Unmarshal(c.Data, &d)
|
|
|
|
data = d
|
|
|
|
case DTEvent:
|
|
|
|
d := DataEvent{}
|
|
|
|
err = json.Unmarshal(c.Data, &d)
|
|
|
|
data = d
|
|
|
|
case DTClient:
|
|
|
|
d := ClientData{}
|
|
|
|
err = json.Unmarshal(c.Data, &d)
|
|
|
|
data = d
|
2019-03-15 07:19:16 +01:00
|
|
|
case DTHidden:
|
|
|
|
d := HiddenMessage{}
|
|
|
|
err = json.Unmarshal(c.Data, &d)
|
|
|
|
data = d
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unhandled data type: %d", c.Type)
|
2019-03-14 20:21:53 +01:00
|
|
|
}
|
2019-03-14 02:24:14 +01:00
|
|
|
|
2019-03-14 20:21:53 +01:00
|
|
|
return data, err
|
2019-03-14 05:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClientData struct {
|
|
|
|
Type ClientDataType
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:08:22 +01:00
|
|
|
func (c ClientData) HTML() string {
|
|
|
|
// Client data is for client to server communication only, so clients should not see this
|
|
|
|
return `<div style="color: red;"><span>The developer messed up. You should not be seeing this.</span></div>`
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
type DataMessage struct {
|
|
|
|
From string
|
|
|
|
Color string
|
|
|
|
Message string
|
2019-03-16 23:11:20 +01:00
|
|
|
Level CommandLevel
|
2019-03-13 18:42:38 +01:00
|
|
|
Type MessageType
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 04:08:22 +01:00
|
|
|
// TODO: Read this HTML from a template somewhere
|
|
|
|
func (dc DataMessage) HTML() string {
|
|
|
|
switch dc.Type {
|
|
|
|
case MsgAction:
|
|
|
|
return `<div style="color:` + dc.Color + `"><span class="name">` + dc.From +
|
|
|
|
`</span> <span class="cmdme">` + dc.Message + `</span></div>`
|
|
|
|
|
|
|
|
case MsgServer:
|
|
|
|
return `<div class="announcement">` + dc.Message + `</div>`
|
|
|
|
|
|
|
|
case MsgError:
|
|
|
|
return `<div class="error">` + dc.Message + `</div>`
|
|
|
|
|
2019-03-15 21:03:02 +01:00
|
|
|
case MsgNotice:
|
|
|
|
return `<div class="notice">` + dc.Message + `</div>`
|
|
|
|
|
2019-03-16 19:37:12 +01:00
|
|
|
case MsgCommandResponse:
|
|
|
|
return `<div class="command">` + dc.Message + `</div>`
|
2019-03-16 23:11:20 +01:00
|
|
|
|
2019-03-15 04:08:22 +01:00
|
|
|
default:
|
2019-03-16 23:11:20 +01:00
|
|
|
badge := ""
|
|
|
|
switch dc.Level {
|
2019-03-24 14:24:57 +01:00
|
|
|
case CmdlMod:
|
2019-03-16 23:11:20 +01:00
|
|
|
badge = `<img src="/static/img/mod.png" class="badge" />`
|
2019-03-24 14:24:57 +01:00
|
|
|
case CmdlAdmin:
|
2019-03-16 23:11:20 +01:00
|
|
|
badge = `<img src="/static/img/admin.png" class="badge" />`
|
|
|
|
}
|
|
|
|
return `<div>` + badge + `<span class="name" style="color:` + dc.Color + `">` + dc.From +
|
2019-03-15 04:08:22 +01:00
|
|
|
`</span><b>:</b> <span class="msg">` + dc.Message + `</span></div>`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func NewChatMessage(name, color, msg string, lvl CommandLevel, msgtype MessageType) ChatData {
|
|
|
|
return ChatData{
|
|
|
|
Type: DTChat,
|
|
|
|
Data: DataMessage{
|
2019-03-15 22:28:29 +01:00
|
|
|
From: name,
|
|
|
|
Color: color,
|
|
|
|
Message: msg,
|
|
|
|
Type: msgtype,
|
2019-03-16 23:11:20 +01:00
|
|
|
Level: lvl,
|
2019-03-21 13:47:40 +01:00
|
|
|
},
|
2019-03-15 04:13:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 06:09:24 +01:00
|
|
|
type DataCommand struct {
|
|
|
|
Command CommandType
|
|
|
|
Arguments []string
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:08:22 +01:00
|
|
|
func (de DataCommand) HTML() string {
|
2019-03-18 03:29:31 +01:00
|
|
|
switch de.Command {
|
|
|
|
case CmdPurgeChat:
|
|
|
|
return `<div class="notice">Chat has been purged by a moderator.</div>`
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
2019-03-15 04:08:22 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func NewChatCommand(command CommandType, args []string) ChatData {
|
|
|
|
return ChatData{
|
|
|
|
Type: DTCommand,
|
|
|
|
Data: DataCommand{
|
2019-03-15 22:28:29 +01:00
|
|
|
Command: command,
|
|
|
|
Arguments: args,
|
2019-03-21 13:47:40 +01:00
|
|
|
},
|
2019-03-15 04:13:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 06:09:24 +01:00
|
|
|
type DataEvent struct {
|
|
|
|
Event EventType
|
|
|
|
User string
|
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:08:22 +01:00
|
|
|
func (de DataEvent) HTML() string {
|
|
|
|
switch de.Event {
|
|
|
|
case EvKick:
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has been kicked.</div>`
|
|
|
|
case EvLeave:
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has left the chat.</div>`
|
|
|
|
case EvBan:
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has been banned.</div>`
|
|
|
|
case EvJoin:
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has joined the chat.</div>`
|
2019-03-19 22:03:34 +01:00
|
|
|
case EvNameChange:
|
|
|
|
names := strings.Split(de.User, ":")
|
|
|
|
if len(names) != 2 {
|
|
|
|
return `<div class="event">Somebody changed their name, but IDK who ` +
|
|
|
|
ParseEmotes("Jebaited") + `.</div>`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
names[0] + `</span> has changed their name to <span class="name" style="color:` +
|
|
|
|
de.Color + `">` + names[1] + `</span>.</div>`
|
|
|
|
case EvNameChangeForced:
|
|
|
|
names := strings.Split(de.User, ":")
|
|
|
|
if len(names) != 2 {
|
|
|
|
return `<div class="event">An admin changed somebody's name, but IDK who ` +
|
|
|
|
ParseEmotes("Jebaited") + `.</div>`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
names[0] + `</span> has had their name changed to <span class="name" style="color:` +
|
|
|
|
de.Color + `">` + names[1] + `</span> by an admin.</div>`
|
2019-03-15 04:08:22 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func NewChatEvent(event EventType, name, color string) ChatData {
|
|
|
|
return ChatData{
|
|
|
|
Type: DTEvent,
|
|
|
|
Data: DataEvent{
|
2019-03-15 22:28:29 +01:00
|
|
|
Event: event,
|
|
|
|
User: name,
|
|
|
|
Color: color,
|
2019-03-21 13:47:40 +01:00
|
|
|
},
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:19:16 +01:00
|
|
|
// DataHidden is for the server to send instructions and data
|
|
|
|
// to the client without the purpose of outputting it on the chat
|
|
|
|
type HiddenMessage struct {
|
|
|
|
Type ClientDataType
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h HiddenMessage) HTML() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func NewChatHiddenMessage(clientType ClientDataType, data interface{}) ChatData {
|
|
|
|
return ChatData{
|
|
|
|
Type: DTHidden,
|
|
|
|
Data: HiddenMessage{
|
2019-03-15 22:28:29 +01:00
|
|
|
Type: clientType,
|
|
|
|
Data: data,
|
2019-03-21 13:47:40 +01:00
|
|
|
},
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:47:40 +01:00
|
|
|
func DecodeData(rawjson string) (ChatDataJSON, error) {
|
|
|
|
var data ChatDataJSON
|
2019-03-14 20:21:53 +01:00
|
|
|
err := json.Unmarshal([]byte(rawjson), &data)
|
|
|
|
return data, err
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|