From e506522eaca5d9a4448c2ad9ee841fd4b1db3454 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 15 Jun 2014 19:26:05 +0200 Subject: [PATCH] Refactoring --- client/index.html | 24 ++++++++++++------------ client/js/chat.js | 12 ++++++++++++ config.js | 27 ++++++++++++++------------- lib/server.js | 31 ++++++++++++++++++++++++------- 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/client/index.html b/client/index.html index f0fdfec..0c2e8a1 100644 --- a/client/index.html +++ b/client/index.html @@ -26,20 +26,20 @@
-
-
-

Shout

-

You need to sign in to continue.

-
-

Password:

- - -
-
+
+
+

Shout

+

You need to sign in to continue.

+
+

Password:

+ + +
+
diff --git a/client/js/chat.js b/client/js/chat.js index 37ca34f..7e581a9 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -1,6 +1,7 @@ $(function() { var chat = $("#chat"); var sidebar = $("#sidebar"); + var windows = $("#windows"); setTimeout(function() { // Enable transitions. @@ -41,6 +42,7 @@ $(function() { var socket = io.connect(""); var events = [ + "debug", "join", "messages", "msg", @@ -62,6 +64,10 @@ $(function() { function event(e, data) { switch (e) { + case "debug": + console.log(data); + break; + case "join": chat.append(render("windows", {windows: [data.chan]})) .find(".window") @@ -295,6 +301,12 @@ $(function() { }); + windows.on("submit", "#sign-in-form", function(e) { + e.preventDefault(); + var password = $("#sign-in-input").val(); + socket.emit("debug", password); + }); + function complete(word) { var words = commands.slice(); var users = $(this).closest(".window") diff --git a/config.js b/config.js index e5b06ce..46c4a4e 100644 --- a/config.js +++ b/config.js @@ -1,15 +1,16 @@ module.exports = { - port: 9000, - messages: 100, - defaults: { - nick: "shout_user", - realname: "http://github.com/erming/shout", - }, - networks: [{ - host: "irc.freenode.org", - port: 6667, - channels: [ - "#shout-irc", - ], - }] + password: "", + port: 9000, + defaults: { + nick: "shout-user", + realname: "http://github.com/erming/shout", + }, + messages: 100, + networks: [{ + host: "irc.freenode.org", + port: 6667, + channels: [ + "#shout-irc", + ], + }] }; diff --git a/lib/server.js b/lib/server.js index 0bced72..cede779 100644 --- a/lib/server.js +++ b/lib/server.js @@ -43,12 +43,12 @@ function listen() { .listen(port); var self = this; - sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) { - s.emit("networks", {networks: networks}); - s.on("input", input); - s.on("fetch", function(data) { - fetch(s, data); - }); + sockets = io.listen(app, {log: 0}).sockets.on("connection", function(socket) { + if (!config.password) { + init.call(socket); + } else { + socket.emit("debug", "auth"); + } }); (config.networks || []).forEach(function(n) { @@ -56,6 +56,17 @@ 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 index(req, res, next) { if (req.url != "/") return next(); fs.readFile("client/index.html", function(err, file) { @@ -116,7 +127,12 @@ function connect(params) { }); } +function debug(data) { + console.log(data); +} + function input(data) { + var socket = this; var target = find(data.id); if (!target) { return; @@ -359,7 +375,8 @@ function input(data) { } } -function fetch(socket, data) { +function fetch(data) { + var socket = this; var target = find(data.id); if (!target) { return;