Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
So far things work.  Needs lots of improvements.
  • Loading branch information
zorchenhimer committed Mar 10, 2019
0 parents commit 3276295
Show file tree
Hide file tree
Showing 17 changed files with 1,676 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.gif
*.png
*.exe

# Linux binary
MovieNight

# Twitch channel info
static/subscriber.json
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#export GOOS=linux
#export GOARCH=386

.PHONY: sync fmt vet

all: vet fmt MovieNight MovieNight.exe

MovieNight.exe: *.go
GOOS=windows GOARCH=amd64 go build -o MovieNight.exe

MovieNight: *.go
GOOS=linux GOARCH=386 go build -o MovieNight

clean:
rm MovieNight.exe MovieNight

fmt:
gofmt -w .

vet:
go vet

sync:
#rsync -v --no-perms --chmod=ugo=rwX -r ./ zorchenhimer@movienight.zorchenhimer.com:/home/zorchenhimer/movienight/
#rsync -v --no-perms --chmod=ugo=rwX -e "ssh -i /c/movienight/movienight-deploy.key" -r ./ zorchenhimer@movienight.zorchenhimer.com:/home/zorchenhimer/movienight/
scp -i /c/movienight/movienight-deploy.key -r . zorchenhimer@movienight.zorchenhimer.com:/home/zorchenhimer/movienight
168 changes: 168 additions & 0 deletions chatclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package main

import (
"fmt"
"html"
"net"
"strings"
"unicode"

"github.com/gorilla/websocket"
)

type Client struct {
name string // Display name
conn *websocket.Conn
belongsTo *ChatRoom
color string
IsMod bool
IsAdmin bool
IsColorForced bool
}

var emotes map[string]string

func ParseEmotes(msg string) string {
words := strings.Split(msg, " ")
newWords := []string{}
for _, word := range words {
word = strings.Trim(word, "[]")

found := false
for key, val := range emotes {
if key == word {
newWords = append(newWords, fmt.Sprintf("<img src=\"/emotes/%s\" title=\"%s\" />", val, key))
//fmt.Printf("[emote] %s\n", val)
found = true
}
}
if !found {
newWords = append(newWords, word)
}
}
return strings.Join(newWords, " ")
}

//Client has a new message to broadcast
func (cl *Client) NewMsg(msg string) {
msg = html.EscapeString(msg)
msg = removeDumbSpaces(msg)
msg = strings.Trim(msg, " ")

// Don't send zero-length messages
if len(msg) == 0 {
return
}

if strings.HasPrefix(msg, "/") {
// is a command
msg = msg[1:len(msg)]
fullcmd := strings.Split(msg, " ")
cmd := strings.ToLower(fullcmd[0])
args := fullcmd[1:len(fullcmd)]

response := commands.RunCommand(cmd, args, cl)
if response != "" {
cl.ServerMessage(response)
return
}

} else {
// Trim long messages
if len(msg) > 400 {
msg = msg[0:400]
}

fmt.Printf("[chat] <%s> %q\n", cl.name, msg)

// Enable links for mods and admins
if cl.IsMod || cl.IsAdmin {
msg = formatLinks(msg)
}

cl.Message(msg)
}
}

// Make links clickable
func formatLinks(input string) string {
newMsg := []string{}
for _, word := range strings.Split(input, " ") {
if strings.HasPrefix(word, "http:&#x2F;&#x2F;") || strings.HasPrefix(word, "https:&#x2F;&#x2F;") {
//word = unescape(word)
word = html.UnescapeString(word)
word = fmt.Sprintf(`<a href="%s" target="_blank">%s</a>`, word, word)
}
newMsg = append(newMsg, word)
}
return strings.Join(newMsg, " ")
}

//Exiting out
func (cl *Client) Exit() {
cl.belongsTo.Leave(cl.name, cl.color)
}

//Sending message block to the client
func (cl *Client) Send(msgs string) {
cl.conn.WriteMessage(websocket.TextMessage, []byte(msgs))
}

// Send server message to this client
func (cl *Client) ServerMessage(msg string) {
msg = ParseEmotes(msg)
cl.Send(`<span class="svmsg">` + msg + `</span><br />`)
}

// Outgoing messages
func (cl *Client) Message(msg string) {
msg = ParseEmotes(msg)
cl.belongsTo.AddMsg(
`<span class="name" style="color:` + cl.color + `">` + cl.name +
`</span><b>:</b> <span class="msg">` + msg + `</span><br />`)
}

// Outgoing /me command
func (cl *Client) Me(msg string) {
msg = ParseEmotes(msg)
cl.belongsTo.AddMsg(fmt.Sprintf(`<span style="color:%s"><span class="name">%s</span> <span class="cmdme">%s</span><br />`, cl.color, cl.name, msg))
}

func (cl *Client) Mod() {
cl.IsMod = true
}

func (cl *Client) Unmod() {
cl.IsMod = false
}

func (cl *Client) Host() string {
host, _, err := net.SplitHostPort(cl.conn.RemoteAddr().String())
if err != nil {
host = "err"
}
return host
}

var dumbSpaces []string = []string{
"\n",
"\t",
"\r",
"\u200b",
}

func removeDumbSpaces(msg string) string {
for _, ds := range dumbSpaces {
msg = strings.ReplaceAll(msg, ds, " ")
}

newMsg := ""
for _, r := range msg {
if unicode.IsSpace(r) {
newMsg += " "
} else {
newMsg += string(r)
}
}
return newMsg
}
Loading

0 comments on commit 3276295

Please sign in to comment.