mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Initial commit
This commit is contained in:
85
KfChatDotNetCli/ChatCliMain.cs
Normal file
85
KfChatDotNetCli/ChatCliMain.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using KfChatDotNetWsClient;
|
||||
using KfChatDotNetWsClient.Models;
|
||||
using KfChatDotNetWsClient.Models.Events;
|
||||
using KfChatDotNetWsClient.Models.Json;
|
||||
using NLog;
|
||||
using Spectre.Console;
|
||||
using Websocket.Client;
|
||||
|
||||
namespace KfChatDotNetCli;
|
||||
|
||||
public class ChatCliMain
|
||||
{
|
||||
private ChatClient _client;
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private int _roomId;
|
||||
|
||||
public ChatCliMain(string xfSessionToken, int roomId)
|
||||
{
|
||||
_roomId = roomId;
|
||||
_client = new ChatClient(new ChatClientConfigModel
|
||||
{
|
||||
WsUri = new Uri("wss://kiwifarms.st/chat.ws"),
|
||||
XfSessionToken = xfSessionToken
|
||||
});
|
||||
|
||||
_client.OnMessages += OnMessages;
|
||||
_client.OnDeleteMessages += OnDeleteMessages;
|
||||
_client.OnUsersJoined += OnUsersJoined;
|
||||
_client.OnUsersParted += OnUsersParted;
|
||||
_client.OnWsReconnect += OnWsReconnected;
|
||||
|
||||
_client.StartWsClient().Wait();
|
||||
_client.JoinRoom(_roomId);
|
||||
|
||||
while (true)
|
||||
{
|
||||
var input = AnsiConsole.Prompt(new TextPrompt<string>("Enter Message:"));
|
||||
_client.SendMessage(input);
|
||||
}
|
||||
// ReSharper disable once FunctionNeverReturns
|
||||
}
|
||||
|
||||
private void OnMessages(object sender, List<MessageModel> messages, MessagesJsonModel jsonPayload)
|
||||
{
|
||||
_logger.Debug($"Received {messages.Count} message(s)");
|
||||
foreach (var message in messages)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"<{message.Author.Username}> {message.Message.EscapeMarkup()} ({message.MessageDate.LocalDateTime.ToShortTimeString()})");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDeleteMessages(object sender, List<int> messageIds)
|
||||
{
|
||||
_logger.Debug($"Received delete event for {messageIds}");
|
||||
foreach (var id in messageIds)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]{id} message deleted![/]");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUsersJoined(object sender, List<UserModel> users, UsersJsonModel jsonPayload)
|
||||
{
|
||||
_logger.Debug($"Received {users.Count} user join events");
|
||||
foreach (var user in users)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[green]{user.Username.EscapeMarkup()} joined![/]");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUsersParted(object sender, List<int> userIds)
|
||||
{
|
||||
_logger.Debug($"Received {userIds.Count} user part events");
|
||||
foreach (var id in userIds)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]{id} left the chat...[/]");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnWsReconnected(object sender, ReconnectionInfo reconnectionInfo)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]Reconnected due to {reconnectionInfo.Type}[/]");
|
||||
AnsiConsole.MarkupLine($"[green]Rejoining {_roomId}[/]");
|
||||
_client.JoinRoom(_roomId);
|
||||
}
|
||||
}
|
||||
28
KfChatDotNetCli/KfChatDotNetCli.csproj
Normal file
28
KfChatDotNetCli/KfChatDotNetCli.csproj
Normal file
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="NLog" Version="5.2.8" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.48.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\KfChatDotNetWsClient\KfChatDotNetWsClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="NLog.config" />
|
||||
<Content Include="NLog.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
15
KfChatDotNetCli/NLog.config
Normal file
15
KfChatDotNetCli/NLog.config
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
|
||||
autoReload="true"
|
||||
throwExceptions="false"
|
||||
internalLogLevel="Off" internalLogFile="~/nlog-internal.log">
|
||||
<targets>
|
||||
<target xsi:type="Console" name="console"/>
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
<logger name="*" minlevel="Info" writeTo="console" />
|
||||
</rules>
|
||||
</nlog>
|
||||
3483
KfChatDotNetCli/NLog.xsd
Normal file
3483
KfChatDotNetCli/NLog.xsd
Normal file
File diff suppressed because it is too large
Load Diff
40
KfChatDotNetCli/Program.cs
Normal file
40
KfChatDotNetCli/Program.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using CommandLine;
|
||||
using NLog;
|
||||
|
||||
namespace KfChatDotNetCli
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public class Options
|
||||
{
|
||||
[Option('t', "token", Required = false, Default = null, HelpText = "XF session token from the 'xf_session' cookie")]
|
||||
public string XfSessionToken { get; set; } = null!;
|
||||
|
||||
[Option("debug", Required = false, Default = false, HelpText = "Enable debug logging")]
|
||||
public bool Debug { get; set; }
|
||||
[Option('r', "room", Required = true, HelpText = "Room ID to join on start")]
|
||||
public int RoomId { get; set; }
|
||||
}
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
Parser.Default.ParseArguments<Options>(args).WithParsed(CliOptions);
|
||||
}
|
||||
|
||||
static void CliOptions(Options options)
|
||||
{
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
|
||||
if (options.Debug)
|
||||
{
|
||||
foreach (var rule in LogManager.Configuration.LoggingRules)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Debug);
|
||||
}
|
||||
}
|
||||
|
||||
new ChatCliMain(options.XfSessionToken, options.RoomId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user