Luski.Net/Luski.net/MainServer.Incoming.cs
JacobTech ea39d93cf5 Friends.
Friends have been copied to the main server.
2023-07-10 14:19:03 -04:00

125 lines
5.1 KiB
C#

using System;
using System.Text;
using System.Text.Json;
using System.Threading;
using JacobTechEncryption;
using Luski.net.Enums;
using Luski.net.JsonTypes;
using Luski.net.JsonTypes.BaseTypes;
using Luski.net.JsonTypes.HTTP;
using Luski.net.JsonTypes.WSS;
using Luski.net.Structures;
using Luski.net.Structures.Main;
using WebSocketSharp;
namespace Luski.net;
public partial class MainServer
{
private void DataFromServer(object? sender, MessageEventArgs e)
{
if (e.IsPing) return;
try
{
Console.WriteLine("From Server: {0}", e.Data);
IncomingWSS? data = JsonSerializer.Deserialize(e.Data, IncomingWSSContext.Default.IncomingWSS);
switch (data?.Type)
{
case DataType.Login:
Console.WriteLine("Pre auth");
WSSLogin n = JsonSerializer.Deserialize(e.Data, WSSLoginContext.Default.WSSLogin)!;
Token = n.Token;
Console.WriteLine("Token: {0}",Token);
break;
case DataType.Error:
if (Token is null)
{
Error = data.Error;
}
else
{
Exception(new Exception(data.Error));
}
break;
case DataType.Message_Create:
if (MessageReceived is not null)
{
MainSocketMessage? m = JsonSerializer.Deserialize<MainSocketMessage>(e.Data);
if (m is not null)
{
m.decrypt(EncryptionHandler.GetChannelKey(m.ChannelID), CancellationToken.None);
_ = MessageReceived.Invoke(m);
}
}
break;
case DataType.Status_Update:
StatusUpdate? SU = JsonSerializer.Deserialize<StatusUpdate>(e.Data);
if (SU is not null)
{
MainSocketRemoteUser after = GetUser(SU.id, MainSocketRemoteUserContext.Default.MainSocketRemoteUser, CancellationToken.None).Result;
after.Status = SU.after;
MainSocketRemoteUser before = after.Clone();
before.Status = SU.before;
StatusUpdate(before, after);
}
break;
case DataType.Friend_Request:
if (ReceivedFriendRequest is not null)
{
FriendRequest? request =
JsonSerializer.Deserialize(e.Data, FriendRequestContext.Default.FriendRequest);
if (request is not null)
_ = ReceivedFriendRequest.Invoke(GetUser(request.Id,
MainSocketRemoteUserContext.Default.MainSocketRemoteUser,
CancellationToken.None).Result);
}
break;
case DataType.Friend_Request_Result:
FriendRequestResult? FRR = JsonSerializer.Deserialize<FriendRequestResult>(e.Data);
if (FRR is not null && FRR.Channel is not null && FRR.Id is not null &&
FRR.Result is not null)
{
MainSocketDMChannel chan = GetChannel((long)FRR.Channel,
MainSocketDMChannelContext.Default.MainSocketDMChannel,
CancellationToken.None).Result;
chans.Add(chan);
MainSocketRemoteUser from1 = GetUser((long)FRR.Id,
MainSocketRemoteUserContext.Default.MainSocketRemoteUser,
CancellationToken.None).Result;
//from1.Channel = chan;
if (FriendRequestResult is not null) _ = FriendRequestResult.Invoke(from1, (bool)FRR.Result);
}
break;
case DataType.Key_Exchange:
try
{
KeyExchange? KE = JsonSerializer.Deserialize<KeyExchange>(e.Data);
if (KE is not null)
{
EncryptionHandler.SetChannelKey(
KE.channel,
Encoding.UTF8.GetString(
Encryption.RSA.Decrypt(
Convert.FromBase64String(KE.key),
EncryptionHandler.myPrivateKey)
)
);
}
}
catch (Exception ex)
{
Exception(ex);
}
break;
default:
Console.WriteLine("Unknown");
break;
}
}
catch (Exception exception)
{
Exception(exception);
}
}
}