348 lines
8.0 KiB
JavaScript
348 lines
8.0 KiB
JavaScript
|
|
// Setup some varibles
|
|
var profiles;
|
|
var connections = [];
|
|
var active_profile;
|
|
var active_chat;
|
|
|
|
// Load the profiles
|
|
fs.readFile("profiles.json", "utf-8", function(err, data)
|
|
{
|
|
try
|
|
{
|
|
// Throw an error if there is one
|
|
if(err) throw err;
|
|
|
|
// Try to load it from the data
|
|
profiles = JSON.parse(data);
|
|
}
|
|
|
|
catch(e)
|
|
{
|
|
// Set the profiles varible to its default value
|
|
profiles = [];
|
|
}
|
|
|
|
// Reload the profiles
|
|
profiles_reload();
|
|
|
|
// Connect to all the profiles
|
|
for(var i=0;i<profiles.length;i++)
|
|
{
|
|
// Connect to the server
|
|
profile_connect(i);
|
|
}
|
|
|
|
document.getElementById("server-menu-profiles").style.bottom =
|
|
document.getElementById("server-menu-config").style.height;
|
|
});
|
|
|
|
function profiles_export()
|
|
{
|
|
// Write the data to the profiles file
|
|
fs.writeFileSync("profiles.json", JSON.stringify(profiles));
|
|
}
|
|
|
|
function profile_save()
|
|
{
|
|
// Get the values from the form
|
|
var hostname = document.getElementById("edit_profile_hostname").value;
|
|
var port = document.getElementById("edit_profile_port").value;
|
|
var username = document.getElementById("edit_profile_username").value;
|
|
var password = document.getElementById("edit_profile_password").value;
|
|
|
|
// Push the profiles data
|
|
profiles.push({
|
|
hostname: hostname,
|
|
port: port,
|
|
username: username,
|
|
password: password
|
|
});
|
|
|
|
// Write the data to the profiles file
|
|
profiles_export();
|
|
|
|
// Reload the profiles
|
|
profiles_reload();
|
|
|
|
// Start a connection
|
|
profile_connect(profiles.length-1);
|
|
}
|
|
|
|
function profile_connect(id)
|
|
{
|
|
// Is the profile set
|
|
if(profiles[id])
|
|
{
|
|
// Connect to the server
|
|
var server = connect(profiles[id], connections.length);
|
|
|
|
// Add the server to the active connections
|
|
connections.push(server);
|
|
}
|
|
|
|
else
|
|
{
|
|
// Push an empty connection
|
|
connections.push(undefined);
|
|
}
|
|
}
|
|
|
|
function port_display(i)
|
|
{
|
|
// Should the port be displayed
|
|
var port_display = "";
|
|
|
|
// Is the port not the default port
|
|
if(profiles[i].port != PORT_DEFAULT)
|
|
{
|
|
// Set the port display varible
|
|
port_display = ":"+profiles[i].port.toString();
|
|
}
|
|
|
|
return port_display;
|
|
}
|
|
|
|
function title(id) {
|
|
return profiles[id].username+"@"+profiles[id].hostname+port_display(id);
|
|
}
|
|
|
|
function profile_switch_to(id)
|
|
{
|
|
// Set the active profile
|
|
active_profile = id;
|
|
|
|
// Create a chats menu
|
|
var chats_menu = "";
|
|
|
|
// Loop over the chats
|
|
for(var i=0;i<connections[id].chats.length;i++)
|
|
{
|
|
// Add to the chats menu
|
|
chats_menu += "<li><a href='#' onclick=chat_switch_to("+i+")>"
|
|
+connections[id].chats[i].name+"</a></li>";
|
|
}
|
|
|
|
// Create a users menu
|
|
var users_menu = "";
|
|
|
|
// Loop over the users
|
|
var users_array = Object.keys(connections[id].users);
|
|
for(var i=0;i<users_array.length;i++)
|
|
{
|
|
// Add to the chats menu
|
|
users_menu += "<li><a href='#'>"+users_array[i]+"</a></li>";
|
|
}
|
|
|
|
// Set the html data
|
|
document.getElementById("chat-main").innerHTML = "\
|
|
<div id='chats-menu' class='menu-vertical'>\
|
|
<ul>\
|
|
"+chats_menu+"\
|
|
<li><a class='add-chat' href='#' onclick='add_chat_menu()'><b>Add</b></a></li>\
|
|
</ul>\
|
|
</div>\
|
|
<div id='chat-area'>\
|
|
</div>\
|
|
<div id='users-menu' class='menu-vertical'>\
|
|
<ul>"+users_menu+"</ul>\
|
|
</div>\
|
|
";
|
|
|
|
// Set the title
|
|
document.title = title(id);
|
|
}
|
|
|
|
function chat_switch_to(id)
|
|
{
|
|
// Create the chat html
|
|
var chat_html = "";
|
|
|
|
// Setup some memory varibles
|
|
var last_chat_user = "";
|
|
|
|
// Loop over the messages
|
|
for(var i=0;i<connections[active_profile].chats[id].messages.length;i++)
|
|
{
|
|
// Set the sender class
|
|
var sender_class = "";
|
|
|
|
if(
|
|
// Is the message sent by this user
|
|
connections[active_profile].chats[id].messages[i].from ==
|
|
connections[active_profile].username
|
|
){
|
|
// Set the sender class
|
|
sender_class = " sender";
|
|
}
|
|
|
|
// Setup the message header varible
|
|
var message_header = "";
|
|
|
|
// Should the chat header be included
|
|
if(last_chat_user != connections[active_profile].chats[id].messages[i].from)
|
|
{
|
|
// Set the last user
|
|
last_chat_user = connections[active_profile].chats[id].messages[i].from;
|
|
|
|
// Include the message header
|
|
message_header = "<p class='message-header'><b>"+
|
|
connections[active_profile].chats[id].messages[i].from+
|
|
"</b></p>";
|
|
}
|
|
|
|
// Add some html data to the chat
|
|
chat_html += "\
|
|
<div class='chat-content-message"+sender_class+"'>"+message_header+"\
|
|
<div><p>"+text_to_html(connections[active_profile].chats[id].messages[i].message)+"</p></div>\
|
|
</div>\
|
|
";
|
|
}
|
|
|
|
// Set the HTML
|
|
document.getElementById("chat-area").innerHTML = "\
|
|
<div id='chat-content'>"+chat_html+"</div>\
|
|
<div id='chat-content-send'>\
|
|
<textarea id='chat-content-send-textarea'></textarea>\
|
|
<button id='chat-content-send-button' onclick='chat_send()'>Send</button>\
|
|
</div>\
|
|
";
|
|
|
|
// Set the title
|
|
document.title = title(active_profile)+" - "
|
|
+connections[active_profile].chats[id].name;
|
|
|
|
// Set the active chat
|
|
active_chat = id;
|
|
|
|
var next = function()
|
|
{
|
|
// Get the chat content element
|
|
var chat_content = document.getElementById("chat-content");
|
|
|
|
// Scroll to the bottom of the div
|
|
chat_content.scrollTop = chat_content.scrollHeight;
|
|
}
|
|
|
|
// Register this to happen when shift enter is pressed in the textarea
|
|
$("#chat-content-send-textarea").on('keydown', function(event)
|
|
{
|
|
// Is the keypress an enter
|
|
if(event.keyCode == 13)
|
|
{
|
|
// Is shift pressed
|
|
if(event.shiftKey)
|
|
{
|
|
// Submit the form
|
|
chat_send();
|
|
}
|
|
}
|
|
});
|
|
|
|
// Try to scroll to the bottom of the div
|
|
next();
|
|
setTimeout(next,1);
|
|
}
|
|
|
|
function chat_send()
|
|
{
|
|
// Get the message
|
|
var message = document.getElementById("chat-content-send-textarea").value;
|
|
|
|
// Is the message not empty and isnt a newline
|
|
if(message.length > 0 && message != "\n")
|
|
{
|
|
// Send the message
|
|
socket_write(
|
|
connections[active_profile].client,
|
|
connections[active_profile].encryption,
|
|
{
|
|
mode: "send_message",
|
|
channel: active_chat,
|
|
message: message
|
|
}
|
|
);
|
|
}
|
|
|
|
// Clear the textarea
|
|
document.getElementById("chat-content-send-textarea").value = "";
|
|
|
|
// Focus the text area really soon
|
|
setTimeout(function()
|
|
{
|
|
// Focus the text area
|
|
document.getElementById("chat-content-send-textarea").focus();
|
|
}, 10);
|
|
}
|
|
|
|
function add_chat_menu()
|
|
{
|
|
// Set the html data
|
|
document.getElementById("chat-area").innerHTML = "\
|
|
<p>\
|
|
<table>\
|
|
<tr><td>Name</td><td><input type='text' id='add-chat-name'></td></tr>\
|
|
</table>\
|
|
<button onclick='add_chat()'>Add</button>\
|
|
</p>\
|
|
";
|
|
}
|
|
|
|
function add_chat()
|
|
{
|
|
// Send the data to the server
|
|
socket_write(
|
|
connections[active_profile].client,
|
|
connections[active_profile].encryption,
|
|
{
|
|
mode: "new_chat",
|
|
name: document.getElementById('add-chat-name').value
|
|
}
|
|
);
|
|
}
|
|
|
|
function profiles_reload()
|
|
{
|
|
// Get the chat menu id
|
|
var chat_menu = document.getElementById("server-menu-profiles");
|
|
|
|
// Reset it
|
|
chat_menu.innerHTML = "";
|
|
|
|
// Loop over the profiles
|
|
for(var i=0;i<profiles.length;i++)
|
|
{
|
|
// Is the profile defined
|
|
if(profiles[i])
|
|
{
|
|
// Create a list item
|
|
var profile = document.createElement("li");
|
|
|
|
// Set the html for the list item
|
|
profile.innerHTML = "<a href='#' onclick='profile_switch_to("+
|
|
i+")'>"+title(i)+"</a>";
|
|
|
|
// Add the profile to the menu
|
|
chat_menu.appendChild(profile);
|
|
}
|
|
}
|
|
}
|
|
|
|
function switch_new_profile()
|
|
{
|
|
// Set the html
|
|
document.title = "New Profile";
|
|
document.getElementById("chat-main").innerHTML = "\
|
|
<p>New Profile:</p>\
|
|
<p>\
|
|
<table>\
|
|
<tr><td>Hostname</td><td><input type='text' id='edit_profile_hostname'></td></tr>\
|
|
<tr><td>Port</td><td><input type='text' id='edit_profile_port' value='22068'></td></tr>\
|
|
<tr><td>Username</td><td><input type='text', id='edit_profile_username'></td></tr>\
|
|
<tr><td>Password</td><td><input type='password' id='edit_profile_password'></td></tr>\
|
|
</table>\
|
|
<button onclick='profile_save()'>Save Profile</button>\
|
|
</p>\
|
|
";
|
|
}
|