Can now accept adding chats

This commit is contained in:
jsrobson10 2019-04-13 10:07:30 +10:00
parent e868b97c1a
commit 1c0a992336
1 changed files with 49 additions and 26 deletions

75
main.js
View File

@ -21,13 +21,13 @@ var db = new sqlite3.Database("./app.db", function(err)
if(err) if(err)
{ {
// Tell the user the error // Tell the user the error
console.log("FATAL: SQlite3 failed to start:", err); console.log("SQlite3 failed to start:", err);
} }
else else
{ {
// Tell the user it worked // 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 // Load some databases
var users = {}; var users = {};
var chats = {}; var chats = [];
// Get the users table // Get the users table
getrows_sqlite3("users", function(data) getrows_sqlite3("users", function(data)
{ {
// Load a user // Load a user
users[data.username] = new Object(); users[data.username] = {
users[data.username].password = data.password; password: data.password
};
}); });
// Get the chats table // Get the chats table
getrows_sqlite3("chats", function(data) getrows_sqlite3("chats", function(data)
{ {
// Create a new object at this chat id // Push the chat
chats[data.id] = new Object(); chats.push({
chats[data.id].messages = []; messages: [],
chats[data.id].messages = data.name; name: data.name
});
}); });
// Create a new messages table // Create a new messages table
@ -102,7 +104,7 @@ getrows_sqlite3("messages", function(data)
chats[data.chat].messages.push(data.message); chats[data.chat].messages.push(data.message);
}); });
console.log("Ready."); console.log("Server ready.");
function toBuffer(bytes) function toBuffer(bytes)
{ {
@ -306,7 +308,7 @@ function socket_init(socket, ondata)
sock.sock = socket; sock.sock = socket;
sock.new = true; sock.new = true;
//console.log("Connection from "+socket.localAdress); console.log("Connection from "+socket.localAddress);
// Wait for data // Wait for data
sock.sock.on('data', function(data) sock.sock.on('data', function(data)
@ -331,7 +333,7 @@ function socket_init(socket, ondata)
// Load the key // Load the key
var key = new node_rsa(); var key = new node_rsa();
key.importKey(data.key, 'public'); key.importKey(data.key, 'public');
console.log("Loaded the RSA key"); //console.log("Loaded the RSA key");
// Get some random bytes // Get some random bytes
random_bytes(settings.encryption_key_size, function(error,string) random_bytes(settings.encryption_key_size, function(error,string)
@ -343,11 +345,11 @@ function socket_init(socket, ondata)
// Make the key // Make the key
sock.key = make_encryption_key(string); sock.key = make_encryption_key(string);
console.log("Created an encryption key") //console.log("Created an encryption key")
// Encrypt the key with RSA // Encrypt the key with RSA
var key_encrypted = key.encrypt(string, 'base64'); var key_encrypted = key.encrypt(string, 'base64');
console.log("Encrypted the key"); //console.log("Encrypted the key");
// Send the key to the client // Send the key to the client
sock.sock.write(send_ordered(JSON.stringify({ sock.sock.write(send_ordered(JSON.stringify({
@ -355,7 +357,7 @@ function socket_init(socket, ondata)
mode: "encryption_key" 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 // Set on disconnect
sock.sock.on('close', function() sock.sock.on('close', function()
{ {
console.log("Connection from "+data.username+" closed.")
// Delete the connection id // Delete the connection id
delete connections[sock.connection_id]; delete connections[sock.connection_id];
}); });
// Get a list of chat keys // Get a list of chat keys
var chat_keys = Object.keys(chats); var chats_client = [];
var chats_client = {};
// Get a list of user keys // Get a list of user keys
var user_keys = Object.keys(users); var user_keys = Object.keys(users);
var users_client = {}; var users_client = {};
// Loop over the chats // Loop over the chats
for(var i=0;i<chat_keys.length;i++) for(var i=0;i<chats.length;i++)
{ {
// Register the key to the temporary varible // Push it to the temporary varible
chats_client[chat_keys[i]] = new Object(); chats_client.push({
name: chats[i].name,
// Set the chat data messages: chats[i].messages
chats_client[chat_keys[i]].name = chats[chat_keys[i]].name; });
chats_client[chat_keys[i]].messages = chat[chat_keys[i]].messages;
} }
// Loop over the users // Loop over the users
for(var i=0;i<user_keys.length;i++) for(var i=0;i<user_keys.length;i++)
{ {
// Register the key to the temporary varible // Register the key to the temporary varible
users_client[user_keys[i]] = new Object(); users_client[user_keys[i]] = {
};
} }
console.log("Sending server data"); //console.log("Sending server data");
// Send the server data // Send the server data
socket_write(sock, { socket_write(sock, {
@ -508,6 +512,25 @@ var server = net.createServer(function(socket)
}); });
} }
} }
// Is the socket logged in
if(sock.logged_in)
{
// Does the user want to make a new chat
if(data.mode == "new_chat")
{
// Make a new chat
chats.push({
messages: [],
name: data.name
});
// Write it to the chats table
sqlite3_insertData("chats", {
name: data.name
})
}
}
}); });
// Setup some varibles // Setup some varibles