Added basic chat, message, and user structure
This commit is contained in:
parent
7b62c5f0d6
commit
31ab993699
107
main.js
107
main.js
|
@ -50,14 +50,13 @@ function sqlite3_getData(name, callback)
|
|||
db.all('SELECT rowid AS id, data FROM '+name, callback);
|
||||
}
|
||||
|
||||
// Load the users
|
||||
var users = {};
|
||||
|
||||
// Create a new table
|
||||
sqlite3_createTable("users", function()
|
||||
function getrows_sqlite3(name, callback)
|
||||
{
|
||||
// Create the table
|
||||
sqlite3_createTable(name, function()
|
||||
{
|
||||
// Load the table
|
||||
sqlite3_getData("users", function(err, rows)
|
||||
sqlite3_getData(name, function(err, rows)
|
||||
{
|
||||
// Throw an error if there is one
|
||||
if(err) throw err;
|
||||
|
@ -68,24 +67,39 @@ sqlite3_createTable("users", function()
|
|||
// Parse the data
|
||||
var data = JSON.parse(rows[i].data);
|
||||
|
||||
// Load a user
|
||||
users[data.username] = new Object();
|
||||
users[data.username].password = data.password;
|
||||
// Call the callback
|
||||
callback(data, i);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Create a logger
|
||||
var logger = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
level: 'debug',
|
||||
handleExceptions: true,
|
||||
json: false,
|
||||
colorize: true,
|
||||
})
|
||||
],
|
||||
exitOnError: false
|
||||
// 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;
|
||||
});
|
||||
|
||||
// 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;
|
||||
});
|
||||
|
||||
// Create a new messages table
|
||||
getrows_sqlite3("messages", function(data)
|
||||
{
|
||||
// Push all the messages to the chats
|
||||
chats[data.chat].messages.push(data.message);
|
||||
});
|
||||
|
||||
console.log("Ready.");
|
||||
|
@ -378,6 +392,9 @@ var server = net.createServer(function(socket)
|
|||
// Does the user want to login
|
||||
if(data.mode == "login")
|
||||
{
|
||||
// Set the logged in varible
|
||||
var logged_in = false;
|
||||
|
||||
// Is the user registered here
|
||||
if(users[data.username])
|
||||
{
|
||||
|
@ -388,15 +405,14 @@ var server = net.createServer(function(socket)
|
|||
sock.username = data.username;
|
||||
|
||||
console.log("Logged "+sock.username+" in.");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
console.log("User failed password test", users[data.username]);
|
||||
// Set logged in
|
||||
logged_in = true;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
// Is register on fail set
|
||||
else if(settings.register_on_fail)
|
||||
{
|
||||
// Make a new user
|
||||
users[data.username] = new Object();
|
||||
|
@ -409,6 +425,47 @@ var server = net.createServer(function(socket)
|
|||
});
|
||||
|
||||
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"
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"encryption_key_size": 10
|
||||
"encryption_key_size": 10,
|
||||
"register_on_fail": true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue