diff --git a/package-lock.json b/package-lock.json index b1d7ad3..a6ac8a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -613,6 +613,11 @@ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=" }, + "dirty": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/dirty/-/dirty-1.1.0.tgz", + "integrity": "sha1-cO3SuZlUHcmXT9Ooy9DGcP4jYHg=" + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/package.json b/package.json index 92aa9f1..b4a9377 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "satyr", - "version": "0.2.0", + "version": "0.3.0", "description": "A livestreaming server.", "license": "AGPL-3.0", "author": "knotteye", @@ -18,6 +18,7 @@ "bcrypt": "^3.0.6", "body-parser": "^1.19.0", "config": "^3.2.2", + "dirty": "^1.1.0", "express": "^4.17.1", "flags": "^0.1.3", "mysql": "^2.17.1", diff --git a/site/send.png b/site/send.png new file mode 100644 index 0000000..7a12595 Binary files /dev/null and b/site/send.png differ diff --git a/src/http.ts b/src/http.ts index 92b4858..f0ee4ff 100644 --- a/src/http.ts +++ b/src/http.ts @@ -4,18 +4,21 @@ import * as bodyparser from "body-parser"; import * as fs from "fs"; import * as socketio from "socket.io"; import * as http from "http"; +import * as dirty from "dirty"; import * as api from "./api"; import * as db from "./database"; const app = express(); const server = http.createServer(app); const io = socketio(server); +const store = dirty(); var njkconf; function init(satyr: any, port: number){ njk.configure('templates', { autoescape: true, express : app, + watch: true }); njkconf ={ sitename: satyr.name, @@ -83,6 +86,9 @@ function init(satyr: any, port: number){ app.get('/changesk', (req, res) => { res.render('changesk.njk', njkconf); }); + app.get('/chat', (req, res) => { + res.render('chat.html', njkconf); + }); //api handlers app.post('/api/register', (req, res) => { api.register(req.body.username, req.body.password, req.body.confirm).then( (result) => { @@ -111,10 +117,84 @@ function init(satyr: any, port: number){ res.status(404).render('404.njk', njkconf); }); //socket.io chat logic - io.on('connection', (socket) => { - + io.on('connection', async (socket) => { + socket.nick = await newNick(socket); + socket.on('JOINROOM', async (data) => { + let t: any = await db.query('select username from users where username='+db.raw.escape(data)); + if(t[0]){ + socket.join(data); + io.to(data).emit('JOINED', {nick: socket.nick}); + } + else socket.emit('ALERT', 'Room does not exist'); + }); + socket.on('LEAVEROOM', (data) => { + socket.leave(data); + io.to(data).emit('LEFT', {nick: socket.nick}); + }); + socket.on('disconnecting', (reason) => { + let rooms = Object.keys(socket.rooms); + for(let i=1;i { + data.nick = data.nick.replace(' ',''); + if(store.get(data.nick)){ + socket.emit('ALERT', 'Nickname is already in use'); + return false; + } + let user = await db.query('select username from users where username='+db.raw.escape(data.nick)); + if(user[0]){ + if(!data.password){ + socket.emit('ALERT','Incorrect username or password'); + return false; + } + if(await db.validatePassword(data.nick, data.password)){ + chgNick(socket, data.nick); + } + else socket.emit('ALERT','Incorrect username or password'); + } + else { + chgNick(socket, data.nick); + } + }); + socket.on('MSG', (data) => { + io.to(data.room).emit('MSG', {nick: socket.nick, msg: data.msg}); + }); + socket.on('KICK', (data) => { + if(socket.nick === data.room){ + //find client with data.nick + let id: string = store.get(data.nick); + if(id){ + let target = io.sockets.connected[id]; + io.in(data.room).emit('ALERT', data.nick+' has been kicked.'); + target.disconnect(true); + } + else socket.emit('ALERT', 'No such user found.'); + } + else socket.emit('ALERT', 'Not authorized to do that.'); + }); }); server.listen(port); } +async function newNick(socket) { + let n: string = 'Guest'+Math.floor(Math.random() * Math.floor(100)); + if(store.get(n)) return newNick(socket); + else { + store.set(n, socket.id); + return n; + } +} +async function chgNick(socket, nick) { + let rooms = Object.keys(socket.rooms); + for(let i=1;i
{% block content %} diff --git a/templates/chat.html b/templates/chat.html new file mode 100644 index 0000000..8ba71ee --- /dev/null +++ b/templates/chat.html @@ -0,0 +1,79 @@ + + + {{ sitename }} Webchat + + + + + + + +
+
  •  
  •  
  • +
    +
    + +
    + \ No newline at end of file diff --git a/templates/user.njk b/templates/user.njk index c077869..2501679 100644 --- a/templates/user.njk +++ b/templates/user.njk @@ -1,13 +1,19 @@ {% extends "base.njk" %} {% block content %} +
    - {{ user }} | {{ streamtitle | escape }} Links | VODs + {{ user }} | {{ streamtitle | escape }} Links | Watch Chat VODs
    -
    - +
    +