Can now accept adding chats
This commit is contained in:
parent
e868b97c1a
commit
1c0a992336
75
main.js
75
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<chat_keys.length;i++)
|
||||
for(var i=0;i<chats.length;i++)
|
||||
{
|
||||
// Register the key to the temporary varible
|
||||
chats_client[chat_keys[i]] = new Object();
|
||||
|
||||
// Set the chat data
|
||||
chats_client[chat_keys[i]].name = chats[chat_keys[i]].name;
|
||||
chats_client[chat_keys[i]].messages = chat[chat_keys[i]].messages;
|
||||
// Push it to the temporary varible
|
||||
chats_client.push({
|
||||
name: chats[i].name,
|
||||
messages: chats[i].messages
|
||||
});
|
||||
}
|
||||
|
||||
// Loop over the users
|
||||
for(var i=0;i<user_keys.length;i++)
|
||||
{
|
||||
// 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
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue