2019-03-13 06:09:24 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChatData struct {
|
|
|
|
Type DataType
|
|
|
|
Data DataInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
type DataMessage struct {
|
|
|
|
From string
|
|
|
|
Color string
|
|
|
|
Message string
|
|
|
|
Type MessageType
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type DataCommand struct {
|
|
|
|
Command CommandType
|
|
|
|
Arguments []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataEvent struct {
|
|
|
|
Event EventType
|
|
|
|
User string
|
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataInterface interface {
|
|
|
|
GetType() DataType
|
|
|
|
HTML() string
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
func (dc DataMessage) GetType() DataType {
|
2019-03-13 06:09:24 +01:00
|
|
|
return DT_CHAT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DataError) GetType() DataType {
|
|
|
|
return DT_ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc DataCommand) GetType() DataType {
|
|
|
|
return DT_COMMAND
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DataEvent) GetType() DataType {
|
|
|
|
return DT_EVENT
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DT_INVALID DataType = iota
|
|
|
|
DT_CHAT // chat message
|
|
|
|
DT_ERROR // something went wrong with the previous request
|
|
|
|
DT_COMMAND // non-chat function
|
|
|
|
DT_EVENT // join/leave/kick/ban events
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseDataType(token json.Token) (DataType, error) {
|
|
|
|
d := fmt.Sprintf("%.0f", token)
|
|
|
|
val, err := strconv.ParseInt(d, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Invalid data type value: %q\n", d)
|
|
|
|
return DT_INVALID, err
|
|
|
|
}
|
|
|
|
return DataType(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CMD_PLAYING CommandType = iota
|
|
|
|
CMD_REFRESHPLAYER
|
|
|
|
CMD_PURGECHAT
|
2019-03-13 18:42:38 +01:00
|
|
|
CMD_HELP
|
2019-03-13 06:09:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
EV_JOIN EventType = iota
|
|
|
|
EV_LEAVE
|
|
|
|
EV_KICK
|
|
|
|
EV_BAN
|
|
|
|
EV_SERVERMESSAGE
|
|
|
|
)
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
type MessageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
MSG_CHAT MessageType = iota // standard chat
|
|
|
|
MSG_ACTION // /me command
|
|
|
|
MSG_SERVER // server message
|
|
|
|
MSG_ERROR
|
|
|
|
)
|
|
|
|
|
2019-03-13 06:09:24 +01:00
|
|
|
// TODO: Read this HTML from a template somewhere
|
2019-03-13 18:42:38 +01:00
|
|
|
func (dc DataMessage) HTML() string {
|
|
|
|
fmt.Printf("message type: %d\n", dc.Type)
|
|
|
|
switch dc.Type {
|
|
|
|
case MSG_ACTION:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div style="color:` + dc.Color + `"><span class="name">` + dc.From +
|
|
|
|
`</span> <span class="cmdme">` + dc.Message + `</span></div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
case MSG_SERVER:
|
2019-03-13 06:09:24 +01:00
|
|
|
return `<div class="announcement">` + dc.Message + `</div>`
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
case MSG_ERROR:
|
|
|
|
return `<div class="error">` + dc.Message + `</div>`
|
|
|
|
|
|
|
|
default:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div><span class="name" style="color:` + dc.Color + `">` + dc.From +
|
|
|
|
`</span><b>:</b> <span class="msg">` + dc.Message + `</span></div>`
|
2019-03-13 18:42:38 +01:00
|
|
|
}
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (de DataEvent) HTML() string {
|
|
|
|
switch de.Event {
|
|
|
|
case EV_KICK:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has been kicked.</div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
case EV_LEAVE:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has left the chat.</div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
case EV_BAN:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has been banned.</div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
case EV_JOIN:
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div class="event"><span class="name" style="color:` + de.Color + `">` +
|
|
|
|
de.User + `</span> has joined the chat.</div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DataError) HTML() string {
|
2019-03-13 19:10:45 +01:00
|
|
|
return `<div class="svmsg"><b>Error</b>: ` + de.Message + `</div>`
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (de DataCommand) HTML() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:42:38 +01:00
|
|
|
func EncodeMessage(name, color, msg string, msgtype MessageType) (string, error) {
|
2019-03-13 06:09:24 +01:00
|
|
|
d := ChatData{
|
|
|
|
Type: DT_CHAT,
|
2019-03-13 18:42:38 +01:00
|
|
|
Data: DataMessage{
|
|
|
|
From: name,
|
|
|
|
Color: color,
|
|
|
|
Message: msg,
|
|
|
|
Type: msgtype,
|
2019-03-13 06:09:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
j, err := jsonifyChatData(d)
|
|
|
|
return j, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeError(message string) (string, error) {
|
|
|
|
d := ChatData{
|
|
|
|
Type: DT_ERROR,
|
|
|
|
Data: DataError{Message: message},
|
|
|
|
}
|
|
|
|
return jsonifyChatData(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeCommand(command CommandType, args []string) (string, error) {
|
|
|
|
d := ChatData{
|
|
|
|
Type: DT_COMMAND,
|
|
|
|
Data: DataCommand{
|
|
|
|
Command: command,
|
|
|
|
Arguments: args,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return jsonifyChatData(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeEvent(event EventType, name, color string) (string, error) {
|
|
|
|
d := ChatData{
|
|
|
|
Type: DT_EVENT,
|
|
|
|
Data: DataEvent{
|
|
|
|
Event: event,
|
|
|
|
User: name,
|
|
|
|
Color: color,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return jsonifyChatData(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonifyChatData(data ChatData) (string, error) {
|
|
|
|
raw, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(raw), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeData(rawjson string) (DataInterface, error) {
|
|
|
|
data := ChatData{}
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(rawjson))
|
|
|
|
|
|
|
|
// Open bracket
|
2019-03-13 18:42:38 +01:00
|
|
|
_, err := decoder.Token()
|
2019-03-13 06:09:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Open bracket token error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for decoder.More() {
|
|
|
|
key, err := decoder.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error decoding token: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprintf("%s", key) == "Type" {
|
|
|
|
value, err := decoder.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error decoding data value: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Type, err = ParseDataType(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing data type: %d", data.Type)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch DataType(data.Type) {
|
|
|
|
case DT_CHAT:
|
2019-03-13 18:42:38 +01:00
|
|
|
d := DataMessage{}
|
2019-03-13 06:09:24 +01:00
|
|
|
if err := decoder.Decode(&d); err != nil {
|
2019-03-13 18:42:38 +01:00
|
|
|
return nil, fmt.Errorf("Unable to decode DataMessage: %s", err)
|
2019-03-13 06:09:24 +01:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
case DT_ERROR:
|
|
|
|
d := DataError{}
|
|
|
|
if err := decoder.Decode(&d); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode DataError: %s", err)
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
case DT_COMMAND:
|
|
|
|
d := DataCommand{}
|
|
|
|
if err := decoder.Decode(&d); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode DataCommand: %s", err)
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
case DT_EVENT:
|
|
|
|
d := DataEvent{}
|
|
|
|
if err := decoder.Decode(&d); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode DataEvent: %s", err)
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid data type: %d", data.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Incomplete data")
|
|
|
|
}
|