chat-client/scripts/profiles.js

464 lines
11 KiB
JavaScript
Raw Normal View History

2019-04-10 15:47:50 +10:00
// Setup some varibles
2019-04-10 15:47:50 +10:00
var profiles;
var connections = [];
var active_profile;
2019-04-13 16:34:16 +10:00
var active_chat;
2019-04-10 15:47:50 +10:00
// 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;
2019-04-10 15:47:50 +10:00
});
function profiles_export()
{
// Write the data to the profiles file
fs.writeFileSync("profiles.json", JSON.stringify(profiles));
}
2019-04-10 15:47:50 +10:00
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;
var proxy_host = document.getElementById("edit_profile_proxy_hostname").value;
var proxy_port = document.getElementById("edit_profile_proxy_port").value;
var mode = document.getElementById("edit_profile_mode").value;
2019-04-10 15:47:50 +10:00
// Create the profile structure
var profile = {
2019-04-10 15:47:50 +10:00
hostname: hostname,
port: port,
username: username,
password: password,
mode: mode
};
// Is this socks5 mode
if(mode == "socks5")
{
// Create a proxy structure
profile.proxy = {
hostname: proxy_host,
port: parseInt(proxy_port)
};
}
// Push the profile
profiles.push(profile);
2019-04-10 15:47:50 +10:00
// Write the data to the profiles file
profiles_export();
2019-04-10 15:47:50 +10:00
// Reload the profiles
profiles_reload();
// Start a connection
profile_connect(profiles.length-1);
}
function profile_connect(id)
{
// Is the profile set
if(profiles[id])
{
2019-04-29 09:04:32 +10:00
// Allocate some varibles for reusing from an old connection
/*var client;
var rsa_task;
// Is there a connection here
if(connections[id])
{
// Reuse some varibles
var client = connections[id].client;
var rsa_task = connections[id].rsa_task;
console.log("rescued");
}
console.log("clirsa", client, rsa_task)*/
// Reconnect to the server
connections[id] = connect(profiles[id], id);
2019-04-29 09:04:32 +10:00
// Set the old varibles
/*connections[id].client = client;
connections[id].rsa_task = rsa_task;*/
}
else
{
// Set an empty connection
connections[id] = undefined;
}
2019-04-10 15:47:50 +10:00
}
2019-04-13 10:10:18 +10:00
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);
}
2019-04-10 15:47:50 +10:00
function profile_switch_to(id)
{
// Switch the profile classes
$("#profile_button_"+active_profile).removeClass("selected");
$("#profile_button_"+id).addClass("selected");
// 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+") id='chat_switch_"+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'>\
2019-04-13 10:10:18 +10:00
<ul>\
"+chats_menu+"\
2019-04-13 16:34:16 +10:00
<li><a class='add-chat' href='#' onclick='add_chat_menu()'><b>Add</b></a></li>\
2019-04-13 10:10:18 +10:00
</ul>\
</div>\
<div id='chat-area'>\
</div>\
<div id='users-menu' class='menu-vertical'>\
2019-04-13 10:10:18 +10:00
<ul>"+users_menu+"</ul>\
</div>\
";
2019-04-13 10:10:18 +10:00
// Set the title
document.title = title(id);
}
function chat_switch_to(id)
{
2019-04-13 16:34:16 +10:00
// Create the chat html
var chat_html = "";
// Setup some memory varibles
var last_chat_user = "";
2019-04-17 16:40:22 +10:00
var last_chat_date = "";
2019-04-13 16:34:16 +10:00
// 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 = "";
2019-04-17 16:40:22 +10:00
// Get the date
var date = new Date(connections[active_profile].chats[id].messages[i].date);
2019-04-17 17:39:03 +10:00
date = (
(date.getMonth()+1)+"/"+
date.getDate()+"/"+
date.getFullYear()+" - "+
date.getHours()+":"+
fixInt(date.getMinutes(), 2)
);
2019-04-17 16:40:22 +10:00
// Should the chat header be included
2019-04-17 16:40:22 +10:00
if(
last_chat_user != connections[active_profile].chats[id].messages[i].from ||
last_chat_date != date
){
// Set the memory varibles
last_chat_user = connections[active_profile].chats[id].messages[i].from;
2019-04-17 16:40:22 +10:00
last_chat_date = date;
// Include the message header
message_header = "<p class='message-header'><b>"+
connections[active_profile].chats[id].messages[i].from+
2019-04-17 16:40:22 +10:00
"</b> "+date+"</p>";
}
2019-04-13 16:34:16 +10:00
// 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>\
2019-04-13 16:34:16 +10:00
</div>\
";
}
// Set the HTML
document.getElementById("chat-area").innerHTML = "\
2019-04-13 16:34:16 +10:00
<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>\
2019-04-13 14:40:31 +10:00
</div>\
";
// Set the title
document.title = title(active_profile)+" - "
+connections[active_profile].chats[id].name;
2019-04-13 16:34:16 +10:00
// Move the active class
$("#chat_switch_"+active_chat).removeClass("selected");
$("#chat_switch_"+id).addClass("selected");
2019-04-13 16:34:16 +10:00
// 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;
// Focus the text area
document.getElementById("chat-content-send-textarea").focus();
}
// 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);
2019-04-13 16:34:16 +10:00
}
function chat_send()
{
// Get the message
var message = document.getElementById("chat-content-send-textarea").value;
2019-04-17 16:40:22 +10:00
// Get the date
var date = new Date();
// Remove some data from the date
date.setMilliseconds(0);
date.setSeconds(0);
// 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,
2019-04-17 16:40:22 +10:00
message: message,
date: Date.parse(date)
}
);
}
2019-04-13 16:34:16 +10:00
// 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);
2019-04-13 10:10:18 +10:00
}
function add_chat_menu()
{
// Deselect the selected chat
$("#chat_switch_"+active_chat).removeClass("selected");
2019-04-13 10:10:18 +10:00
// 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
}
);
2019-04-10 15:47:50 +10:00
}
function profiles_reload()
{
console.log("Profiles reloaded");
2019-04-10 15:47:50 +10:00
// Get the chat menu id
var chat_menu = document.getElementById("server-menu-profiles");
2019-04-10 15:47:50 +10:00
// 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");
2019-04-10 15:47:50 +10:00
// Create the disabled class
var disabled_class = "class='disabled'";
// Is there a connection to the server
if(connections[i])
{
// Is the connection logged in
if(connections[i].logged_in)
{
// Remove the disabled class and make the link clickable
disabled_class = "href='#' onclick='profile_switch_to("+i+")'";
}
}
// Set the html for the list item
profile.innerHTML = "<a "+disabled_class+" id='profile_button_"+i+"'>"+title(i)+"</a>";
2019-04-10 15:47:50 +10:00
// Add the profile to the menu
chat_menu.appendChild(profile);
}
2019-04-10 15:47:50 +10:00
}
}
function edit_profile_mode_update()
{
// Get the mode
var mode = document.getElementById("edit_profile_mode").value;
// Is the mode direct
if(mode == "direct")
{
// Clear the extra settings
$(".edit_profile_proxy_section").addClass("off");
$(".edit_profile_proxy_section").removeClass("on");
}
// Is the mode socks5
if(mode == "socks5")
{
// Set some extra settings
$(".edit_profile_proxy_section").addClass("on");
$(".edit_profile_proxy_section").removeClass("off");
}
}
function switch_new_profile()
{
// Set the html
2019-04-13 10:10:18 +10:00
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>\
<tr><td>Mode</td><td>\
<select id='edit_profile_mode' onclick='edit_profile_mode_update()'>\
<option value='direct'>Direct</option>\
<option value='socks5'>Socks5</option>\
</select>\
</td></tr>\
<tr class='edit_profile_proxy_section off'><td>Proxy Host</td><td>\
<input type='text' id='edit_profile_proxy_hostname' value='localhost'></td></tr>\
<tr class='edit_profile_proxy_section off'><td>Proxy Port</td><td>\
<input type='text' id='edit_profile_proxy_port' value='9050'></td></tr>\
</table>\
<button onclick='profile_save()'>Save Profile</button>\
</p>\
";
}