2019-03-10 16:42:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-12 06:53:51 +01:00
|
|
|
"html/template"
|
2019-03-10 16:42:12 +01:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2019-03-12 06:53:51 +01:00
|
|
|
"strings"
|
2019-03-10 16:42:12 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
//global variable for handling all chat traffic
|
|
|
|
var chat ChatRoom
|
|
|
|
|
|
|
|
// Serving static files
|
|
|
|
func wsStaticFiles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/favicon.ico":
|
|
|
|
http.ServeFile(w, r, "./static/favicon.png")
|
|
|
|
return
|
|
|
|
case "/justchat":
|
|
|
|
http.ServeFile(w, r, "./static/justchat.html")
|
|
|
|
return
|
|
|
|
case "/justvideo":
|
|
|
|
http.ServeFile(w, r, "./static/justvideo.html")
|
|
|
|
return
|
2019-03-11 17:05:00 +01:00
|
|
|
|
|
|
|
// TODO: use a template for this, lol.
|
|
|
|
case "/help":
|
|
|
|
w.Write([]byte(helpPage(false, false)))
|
|
|
|
return
|
|
|
|
case "/modhelp":
|
|
|
|
w.Write([]byte(helpPage(true, false)))
|
|
|
|
return
|
|
|
|
case "/adminhelp":
|
|
|
|
w.Write([]byte(helpPage(true, true)))
|
|
|
|
return
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
goodPath := r.URL.Path[8:len(r.URL.Path)]
|
|
|
|
fmt.Printf("[static] serving %q from folder ./static/\n", goodPath)
|
|
|
|
|
|
|
|
http.ServeFile(w, r, "./static/"+goodPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsEmotes(w http.ResponseWriter, r *http.Request) {
|
|
|
|
emotefile := filepath.Base(r.URL.Path)
|
|
|
|
//fmt.Printf("serving emote: %s\n", emotefile)
|
|
|
|
http.ServeFile(w, r, "./static/emotes/"+emotefile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handling the websocket
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
CheckOrigin: func(r *http.Request) bool { return true }, //not checking origin
|
|
|
|
}
|
|
|
|
|
|
|
|
//this is also the handler for joining to the chat
|
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error upgrading to websocket:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2019-03-12 04:15:42 +01:00
|
|
|
var client *Client
|
|
|
|
|
2019-03-10 16:42:12 +01:00
|
|
|
//first message has to be the name
|
2019-03-12 04:15:42 +01:00
|
|
|
// loop through name since websocket is opened once
|
|
|
|
for client == nil {
|
|
|
|
_, msg, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[handler] Client closed connection: %s\n", conn.RemoteAddr().String())
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := string(msg)
|
|
|
|
client, err = chat.Join(name, conn)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case UserFormatError, UserTakenError:
|
|
|
|
fmt.Printf("[handler] %v\n", err)
|
|
|
|
case BannedUserError:
|
|
|
|
fmt.Printf("[BAN] %v\n", err)
|
|
|
|
// close connection since banned users shouldn't be connecting
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 16:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//then watch for incoming messages
|
|
|
|
for {
|
|
|
|
_, msg, err := conn.ReadMessage()
|
|
|
|
if err != nil { //if error then assuming that the connection is closed
|
|
|
|
client.Exit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.NewMsg(string(msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
2019-03-12 06:53:51 +01:00
|
|
|
|
|
|
|
func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t, err := template.ParseFiles("./static/index.html")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERR] could not parse template file, %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
Title string
|
|
|
|
Video, Chat bool
|
|
|
|
}
|
|
|
|
|
|
|
|
data := Data{
|
|
|
|
Title: "Movie Night!",
|
|
|
|
Video: true,
|
|
|
|
Chat: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
path := strings.Split(strings.TrimLeft(r.URL.Path, "/"), "/")
|
|
|
|
fmt.Printf("%#v\n", path)
|
|
|
|
if path[0] == "chat" {
|
|
|
|
data.Video = false
|
|
|
|
data.Title += " - chat"
|
|
|
|
|
|
|
|
} else if path[0] == "video" {
|
|
|
|
data.Chat = false
|
|
|
|
data.Title += " - video"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(data)
|
|
|
|
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERR] could not execute file, %v", err)
|
|
|
|
}
|
|
|
|
}
|