This commit is contained in:
tyuXX 2026-10-10 16:40:35 +00:00
commit f25ba8adcd
64 changed files with 5959 additions and 0 deletions

View file

@ -0,0 +1,146 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using GsDChat.Client.Network;
using GsDChat.Client.Rendering;
using GsDChat.Client.State;
namespace GsDChat.Client;
public class Program
{
public static async Task Main(string[] args)
{
string serverUrl = "http://localhost:5000";
string? username = null;
string? password = null;
string? serverPassword = null;
bool doRegister = false;
bool allowInsecure = false;
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--server" && i + 1 < args.Length) serverUrl = args[i + 1];
if (args[i] == "--username" && i + 1 < args.Length) username = args[i + 1];
if (args[i] == "--password" && i + 1 < args.Length) password = args[i + 1];
if (args[i] == "--server-password" && i + 1 < args.Length) serverPassword = args[i + 1];
if (args[i] == "--register") doRegister = true;
if (args[i] == "--insecure") allowInsecure = true;
}
Console.WriteLine($"{Ansi.Bold}{Ansi.Cyan}========================================");
Console.WriteLine(" GsDChat - Private Community TUI");
Console.WriteLine($"========================================{Ansi.Reset}");
var state = new ClientState
{
ServerUrl = serverUrl
};
using var connection = new ChatClientConnection(state, allowInsecure);
Console.Write($"Connecting to server at {serverUrl}... ");
var info = await connection.GetServerInfoAsync();
if (info == null)
{
Console.WriteLine($"{Ansi.BrightRed}Failed to reach server!{Ansi.Reset}");
Console.WriteLine("Ensure the server is running and the URL is correct.");
return;
}
Console.WriteLine($"{Ansi.BrightGreen}Connected!{Ansi.Reset}");
Console.WriteLine($"Server: {info.ServerName} (v{info.Version})");
if (!string.IsNullOrEmpty(info.Motd))
{
Console.WriteLine($"MOTD: {info.Motd}");
}
if (info.RequiresServerPassword && string.IsNullOrEmpty(serverPassword))
{
Console.Write("Enter Server Password: ");
serverPassword = ReadMaskedPassword();
}
if (string.IsNullOrEmpty(username))
{
Console.Write("\nChoose action: [L]ogin or [R]egister: ");
var choice = Console.ReadLine()?.Trim().ToUpperInvariant();
if (choice == "R" || choice == "REGISTER")
{
doRegister = true;
}
Console.Write("Username: ");
username = Console.ReadLine()?.Trim();
Console.Write("Password: ");
password = ReadMaskedPassword();
}
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{
Console.WriteLine($"{Ansi.BrightRed}Username and password are required.{Ansi.Reset}");
return;
}
Console.WriteLine(doRegister ? "Registering account..." : "Logging in...");
var authResult = doRegister
? await connection.RegisterAsync(username, password, serverPassword)
: await connection.LoginAsync(username, password, serverPassword);
if (!authResult.Success || string.IsNullOrEmpty(authResult.Token))
{
Console.WriteLine($"{Ansi.BrightRed}Authentication failed: {authResult.ErrorMessage}{Ansi.Reset}");
return;
}
Console.WriteLine($"{Ansi.BrightGreen}Success! Welcome @{authResult.Username} [{authResult.Role}]{Ansi.Reset}");
state.Token = authResult.Token;
state.CurrentUserId = authResult.UserId ?? "";
state.CurrentUsername = authResult.Username ?? username;
state.CurrentUserRole = authResult.Role ?? "Member";
await connection.StartWebSocketAsync();
// Launch TUI
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
var ui = new ConsoleUi(state, connection);
await ui.RunAsync(cts.Token);
}
private static string ReadMaskedPassword()
{
string pwd = "";
while (true)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (key.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd = pwd[..^1];
Console.Write("\b \b");
}
}
else if (!char.IsControl(key.KeyChar))
{
pwd += key.KeyChar;
Console.Write("*");
}
}
return pwd;
}
}