From 86e4ad770feafd368c97c477dd4af1b14ebb79b5 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 15 Jun 2014 21:18:45 +0200 Subject: [PATCH] Added sign in --- client/css/style.css | 3 -- client/js/chat.js | 15 ++++--- lib/server.js | 103 +++++++++++++++++++++++-------------------- 3 files changed, 65 insertions(+), 56 deletions(-) diff --git a/client/css/style.css b/client/css/style.css index a73be8d..8d3d8ef 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -120,9 +120,6 @@ button { #sidebar a:hover .close { opacity: .4; } -#sidebar a[href="#sign-in"] { - display: none; -} #sidebar h1, #sidebar h2 { color: #fff; diff --git a/client/js/chat.js b/client/js/chat.js index 7e581a9..d57cd40 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -1,8 +1,7 @@ $(function() { var chat = $("#chat"); var sidebar = $("#sidebar"); - var windows = $("#windows"); - + setTimeout(function() { // Enable transitions. $("body").removeClass("preload"); @@ -42,6 +41,7 @@ $(function() { var socket = io.connect(""); var events = [ + "auth", "debug", "join", "messages", @@ -64,6 +64,11 @@ $(function() { function event(e, data) { switch (e) { + case "auth": + chat.add($("#networks")).empty(); + $("#sign-in").addClass("active"); + break; + case "debug": console.log(data); break; @@ -298,13 +303,11 @@ $(function() { viewport.removeClass("lt"); }); } - }); - windows.on("submit", "#sign-in-form", function(e) { + $("#sign-in-form").on("submit", function(e) { e.preventDefault(); - var password = $("#sign-in-input").val(); - socket.emit("debug", password); + socket.emit("auth", $("#sign-in-input").val()); }); function complete(word) { diff --git a/lib/server.js b/lib/server.js index cede779..b04413c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -42,13 +42,11 @@ function listen() { .use(http.static("client")) .listen(port); - var self = this; sockets = io.listen(app, {log: 0}).sockets.on("connection", function(socket) { - if (!config.password) { - init.call(socket); - } else { - socket.emit("debug", "auth"); - } + init.call( + socket, + !!config.password + ); }); (config.networks || []).forEach(function(n) { @@ -56,15 +54,20 @@ function listen() { }); } -function init() { - var socket = this; - socket.on("debug", debug); - socket.on("input", input); - socket.on("fetch", fetch); - socket.emit( - "networks", - {networks: networks} - ); +function init(login) { + if (login) { + this.on("auth", auth); + this.emit("auth"); + } else { + this.join("chat"); + this.on("debug", debug); + this.on("input", input); + this.on("fetch", fetch); + this.emit( + "networks", + {networks: networks} + ); + } } function index(req, res, next) { @@ -106,7 +109,7 @@ function connect(params) { }); networks.push(network); - sockets.emit("networks", {networks: networks}); + sockets.in("chat").emit("networks", {networks: networks}); client.nick(params.nick); client.user(params.nick, params.realname); @@ -127,6 +130,13 @@ function connect(params) { }); } +function auth(password) { + if (password == config.password) { + this.disable = false; + init.call(this); + } +} + function debug(data) { console.log(data); } @@ -175,7 +185,7 @@ function input(data) { text: text, }); chan.addMsg(msg) - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -206,7 +216,7 @@ function input(data) { text: text, }); chan.addMsg(msg) - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -227,7 +237,7 @@ function input(data) { text: text, }); chan.addMsg(msg) - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -251,7 +261,6 @@ function input(data) { if (!args[2] || args[2].charAt(0) == "#") { client.join(args.slice(1)); } else { - console.log(args[1], args[2]); client.join( args[1], args[2] // Password @@ -278,7 +287,7 @@ function input(data) { var id = chan.id; if (chan.type == "query" || !chan.users.length) { remove(id); - sockets.emit("part", { + sockets.in("chat").emit("part", { id: id, }); } else if (client) { @@ -360,7 +369,7 @@ function input(data) { case "disconnect": if (client) { networks = _.without(networks, network); - sockets.emit("networks", {networks: networks}); + sockets.in("chat").emit("networks", {networks: networks}); client.quit(); } break; @@ -397,7 +406,7 @@ function event(e, data) { switch (e) { case "errors": - sockets.emit("msg", { + sockets.in("chat").emit("msg", { msg: new Msg({ type: "error", from: "-!-", @@ -419,7 +428,7 @@ function event(e, data) { name: data.channel, }); this.addChan(chan); - sockets.emit("join", { + sockets.in("chat").emit("join", { id: this.id, chan: chan, }); @@ -427,7 +436,7 @@ function event(e, data) { var users = chan.users; users.push(new User({name: data.nick})); chan.sortUsers(); - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: users, }); @@ -436,7 +445,7 @@ function event(e, data) { type: "join", }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -452,7 +461,7 @@ function event(e, data) { } else { chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.client})); } - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: chan.users, }); @@ -462,7 +471,7 @@ function event(e, data) { text: data.client, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -485,7 +494,7 @@ function event(e, data) { text: data.mode + " " + data.client, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -501,7 +510,7 @@ function event(e, data) { text: m, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -517,7 +526,7 @@ function event(e, data) { type: "query", }); this.addChan(chan); - sockets.emit("join", { + sockets.in("chat").emit("join", { id: this.id, chan: chan, }); @@ -538,7 +547,7 @@ function event(e, data) { text: text, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -554,7 +563,7 @@ function event(e, data) { chan.addUser(new User(n)); }); chan.sortUsers(); - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: chan.users, }); @@ -568,7 +577,7 @@ function event(e, data) { text: "You're now known as " + data["new"], }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -580,7 +589,7 @@ function event(e, data) { } user.name = data["new"]; chan.sortUsers(); - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: chan.users, }); @@ -590,7 +599,7 @@ function event(e, data) { text: data["new"], }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -609,7 +618,7 @@ function event(e, data) { text: data.message, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -622,12 +631,12 @@ function event(e, data) { } if (data.nick == this.client.me) { remove(chan.id); - sockets.emit("part", { + sockets.in("chat").emit("part", { id: chan.id, }); } else { chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.nick})); - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: chan.users, }); @@ -636,7 +645,7 @@ function event(e, data) { from: data.nick, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -650,7 +659,7 @@ function event(e, data) { return; } chan.users = _.without(chan.users, user); - sockets.emit("users", { + sockets.in("chat").emit("users", { id: chan.id, users: chan.users, }); @@ -659,7 +668,7 @@ function event(e, data) { from: data.nick, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -678,7 +687,7 @@ function event(e, data) { text: data.topic, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -692,7 +701,7 @@ function event(e, data) { text: "You're now known as " + data, }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -709,7 +718,7 @@ function event(e, data) { name: data.nickname, }); this.addChan(chan); - sockets.emit("join", { + sockets.in("chat").emit("join", { id: this.id, chan: chan, }); @@ -732,7 +741,7 @@ function event(e, data) { text: key + " " + data[k], }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, }); @@ -743,7 +752,7 @@ function event(e, data) { text: "End of /WHOIS list.", }); chan.addMsg(msg); - sockets.emit("msg", { + sockets.in("chat").emit("msg", { id: chan.id, msg: msg, });