Added basic chat, message, and user structure

This commit is contained in:
jsrobson10 2019-04-12 17:09:24 +10:00
parent 7b62c5f0d6
commit 31ab993699
2 changed files with 93 additions and 35 deletions

125
main.js
View File

@ -50,42 +50,56 @@ function sqlite3_getData(name, callback)
db.all('SELECT rowid AS id, data FROM '+name, callback); db.all('SELECT rowid AS id, data FROM '+name, callback);
} }
// Load the users function getrows_sqlite3(name, callback)
var users = {};
// Create a new table
sqlite3_createTable("users", function()
{ {
// Load the table // Create the table
sqlite3_getData("users", function(err, rows) sqlite3_createTable(name, function()
{ {
// Throw an error if there is one // Load the table
if(err) throw err; sqlite3_getData(name, function(err, rows)
// Loop over the rows
for(var i=0;i<rows.length;i++)
{ {
// Parse the data // Throw an error if there is one
var data = JSON.parse(rows[i].data); if(err) throw err;
// Load a user // Loop over the rows
users[data.username] = new Object(); for(var i=0;i<rows.length;i++)
users[data.username].password = data.password; {
} // Parse the data
var data = JSON.parse(rows[i].data);
// Call the callback
callback(data, i);
}
});
}); });
}
// Load some databases
var users = {};
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;
}); });
// Create a logger // Get the chats table
var logger = winston.createLogger({ getrows_sqlite3("chats", function(data)
transports: [ {
new winston.transports.Console({ // Create a new object at this chat id
level: 'debug', chats[data.id] = new Object();
handleExceptions: true, chats[data.id].messages = [];
json: false, chats[data.id].messages = data.name;
colorize: true, });
})
], // Create a new messages table
exitOnError: false getrows_sqlite3("messages", function(data)
{
// Push all the messages to the chats
chats[data.chat].messages.push(data.message);
}); });
console.log("Ready."); console.log("Ready.");
@ -378,6 +392,9 @@ var server = net.createServer(function(socket)
// Does the user want to login // Does the user want to login
if(data.mode == "login") if(data.mode == "login")
{ {
// Set the logged in varible
var logged_in = false;
// Is the user registered here // Is the user registered here
if(users[data.username]) if(users[data.username])
{ {
@ -388,15 +405,14 @@ var server = net.createServer(function(socket)
sock.username = data.username; sock.username = data.username;
console.log("Logged "+sock.username+" in."); console.log("Logged "+sock.username+" in.");
}
else // Set logged in
{ logged_in = true;
console.log("User failed password test", users[data.username]);
} }
} }
else // Is register on fail set
else if(settings.register_on_fail)
{ {
// Make a new user // Make a new user
users[data.username] = new Object(); users[data.username] = new Object();
@ -409,6 +425,47 @@ var server = net.createServer(function(socket)
}); });
console.log("Creating account "+data.username+"."); console.log("Creating account "+data.username+".");
// Set logged in
logged_in = true;
}
// Is the user now logged in
if(logged_in)
{
// Get a list of chat keys
var chat_keys = Object.keys(chats);
var chats_client = {};
// Loop over them
for(var i=0;i<chat_keys.length;i++)
{
// Register the key to the temporary varible
chats_client[chat_keys[i]] = new Object();
// Set the chat name
chats_client[chat_keys[i]].name = chats[chat_keys[i]].name;
chats_client[chat_keys[i]].messages = chat[chat_keys[i]].messages;
}
console.log("Sending list of chats");
// Send the list of chats
socket_write(sock, {
mode: "chat_list",
chats: chats_client
});
}
else
{
console.log("Authentication failed.");
// Send back an error message
socket_write(sock, {
mode: "error",
error: "auth"
});
} }
} }
}); });

View File

@ -1,3 +1,4 @@
{ {
"encryption_key_size": 10 "encryption_key_size": 10,
"register_on_fail": true
} }