diff --git a/main.js b/main.js index 55716cd..a6ab878 100644 --- a/main.js +++ b/main.js @@ -21,13 +21,13 @@ var db = new sqlite3.Database("./app.db", function(err) if(err) { // Tell the user the error - console.log("FATAL: SQlite3 failed to start:", err); + console.log("SQlite3 failed to start:", err); } else { // Tell the user it worked - console.log("INFO: SQlite3 ready."); + console.log("SQlite3 ready."); } }); @@ -76,23 +76,25 @@ function getrows_sqlite3(name, callback) // Load some databases var users = {}; -var chats = {}; +var chats = []; // Get the users table getrows_sqlite3("users", function(data) { // Load a user - users[data.username] = new Object(); - users[data.username].password = data.password; + users[data.username] = { + password: data.password + }; }); // Get the chats table getrows_sqlite3("chats", function(data) { - // Create a new object at this chat id - chats[data.id] = new Object(); - chats[data.id].messages = []; - chats[data.id].messages = data.name; + // Push the chat + chats.push({ + messages: [], + name: data.name + }); }); // Create a new messages table @@ -102,7 +104,7 @@ getrows_sqlite3("messages", function(data) chats[data.chat].messages.push(data.message); }); -console.log("Ready."); +console.log("Server ready."); function toBuffer(bytes) { @@ -306,7 +308,7 @@ function socket_init(socket, ondata) sock.sock = socket; sock.new = true; - //console.log("Connection from "+socket.localAdress); + console.log("Connection from "+socket.localAddress); // Wait for data sock.sock.on('data', function(data) @@ -331,7 +333,7 @@ function socket_init(socket, ondata) // Load the key var key = new node_rsa(); key.importKey(data.key, 'public'); - console.log("Loaded the RSA key"); + //console.log("Loaded the RSA key"); // Get some random bytes random_bytes(settings.encryption_key_size, function(error,string) @@ -343,11 +345,11 @@ function socket_init(socket, ondata) // Make the key sock.key = make_encryption_key(string); - console.log("Created an encryption key") + //console.log("Created an encryption key") // Encrypt the key with RSA var key_encrypted = key.encrypt(string, 'base64'); - console.log("Encrypted the key"); + //console.log("Encrypted the key"); // Send the key to the client sock.sock.write(send_ordered(JSON.stringify({ @@ -355,7 +357,7 @@ function socket_init(socket, ondata) mode: "encryption_key" }))); - console.log("Sent the key to the client"); + //console.log("Sent the key to the client"); }); } } @@ -457,37 +459,39 @@ var server = net.createServer(function(socket) // Set on disconnect sock.sock.on('close', function() { + console.log("Connection from "+data.username+" closed.") + // Delete the connection id delete connections[sock.connection_id]; }); // Get a list of chat keys - var chat_keys = Object.keys(chats); - var chats_client = {}; + var chats_client = []; // Get a list of user keys var user_keys = Object.keys(users); var users_client = {}; // Loop over the chats - for(var i=0;i