Luski.Net/Luski.net/PublicServer.Incoming.cs

196 lines
8.4 KiB
C#
Raw Normal View History

using System;
2024-03-20 23:18:34 -04:00
using System.Collections.Generic;
2024-08-27 10:57:22 -04:00
using System.Linq;
using System.Text.Json;
2024-08-27 10:57:22 -04:00
using System.Threading.Tasks;
2024-03-20 23:18:34 -04:00
using JacobTechEncryption;
using JacobTechEncryption.Enums;
using Luski.net.Enums;
using Luski.net.JsonTypes.BaseTypes;
using Luski.net.JsonTypes.WSS;
2024-03-20 23:18:34 -04:00
using Luski.net.Structures;
using Luski.net.Structures.Public;
2024-08-27 10:57:22 -04:00
using Luski.Shared.PublicServers.V1.Enums;
2024-03-20 23:18:34 -04:00
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
2024-08-27 10:57:22 -04:00
using Luski.Shared.PublicServers.V1.Shared;
using WebSocketSharp;
2024-03-20 23:18:34 -04:00
using File = System.IO.File;
using DataType = Luski.Shared.PublicServers.V1.Enums.DataType;
namespace Luski.net;
public partial class PublicServer
{
private void DataFromServer(object? sender, MessageEventArgs e)
{
if (e.IsPing) return;
2024-03-20 23:18:34 -04:00
if (PrintServerMessages)
{
Console.WriteLine(e.Data);
}
try
{
BetterIncomingWSS? data = JsonSerializer.Deserialize(e.Data, BetterIncomingWSSContext.Default.BetterIncomingWSS);
switch (data?.Type)
{
case DataType.Token:
WSSLogin n = JsonSerializer.Deserialize(e.Data, WSSLoginContext.Default.WSSLogin)!;
File.WriteAllText(Storage.GetStorageDirectory(StorageDirectory.StorageInfo) + "token",
n.SessionToken);
Token = n.Token;
break;
2024-08-27 10:57:22 -04:00
case DataType.StatusUpdate:
StatusEvent se = JsonSerializer.Deserialize(data.Data!.ToString()!, StatusEventContext.Default.StatusEvent)!;
SocketUser u;
if (se.User == User.Id) u = User;
else
{
u = GetUser<SocketUser>(se.User);
u.Status = se.After;
}
if (StatusUpdate is not null)
{
StatusUpdate.Invoke(se.Before, u);
}
break;
case DataType.Role:
RoleEvent re = JsonSerializer.Deserialize(data.Data!.ToString()!, RoleEventContext.Default.RoleEvent)!;
Role[] ra = roles.Where(s => s.ID == re.ID).ToArray();
Role r;
bool @new = false;
if (ra.Length > 0)
{
r = ra[0];
if (re.ServerPermissions is not null) r.ServerPermissions = re.ServerPermissions.Value;
if (re.Description is not null) r.Description = re.Description;
if (re.DisplayName is not null) r.DisplayName = re.DisplayName;
if (re.ColorType is not null) r.ColorType = re.ColorType.Value;
if (re.Color is not null)
{
if (r.ColorType == ColorType.Full)
{
Color nc = new(re.Color);
r.Colors = new []{nc};
}
else
{
List<Color> cols = new();
for (int i = 0; i < re.Color.Length - 7; i+=8)
{
cols.Add(new(re.Color[i..(i+8)]));
}
r.Colors = cols.ToArray();
}
}
if (re.Index is not null) r.Index = re.Index.Value;
}
else
{
@new = true;
Task<Role> rr = GetRole(re.ID);
rr.Wait();
r = rr.Result;
}
if (RoleEvent is not null) _ = RoleEvent.Invoke(@new, r);
break;
case DataType.RoleMember:
RoleMemberEvent rme = JsonSerializer.Deserialize(data.Data!.ToString()!, RoleMemberEventContext.Default.RoleMemberEvent)!;
break;
case DataType.RoleDelete:
break;
2024-03-20 23:18:34 -04:00
case DataType.MessageCreate:
MessageSTC smsg = JsonSerializer.Deserialize(data.Data!.ToString()!, MessageSTCContext.Default.MessageSTC)!;
List<long> fl = new();
2024-08-27 10:57:22 -04:00
List<SocketFile> sf = new();
foreach (ServerFileInfoSTC v in smsg.Files)
2024-03-20 23:18:34 -04:00
{
2024-08-27 10:57:22 -04:00
if (v.NameKey == 0)
{
if (string.IsNullOrEmpty(v.Name))
{
v.Name = "";
}
else v.Name = Encryption.Generic.Encoders[(int)v.NameEncoder]
.GetString(Convert.FromBase64String(v.Name));
}
else
{
LocalKeyInfo key = EncryptionHandler.GetKey(v.NameKey);
switch (key.EncryptionType)
{
case EncryptionType.RSA:
v.Name = Encryption.RSA.Decrypt(Convert.FromBase64String(v.Name), key.Key,
v.NameEncoder);
break;
default:
v.Name = Encryption.Generic.Encoders[(int)v.NameEncoder]
.GetString(Convert.FromBase64String(v.Name));
break;
}
}
fl.Add(v.ID);
sf.Add(new()
{
ID = v.ID,
Size = v.Size,
Name = v.Name,
Encoder = v.Encoder,
NameEncoder = v.NameEncoder,
Key = v.Key,
NameKey = v.NameKey,
Server = this
});
2024-03-20 23:18:34 -04:00
}
if (smsg.EncryptionKey == 0)
{
if (string.IsNullOrEmpty(smsg.Context))
{
smsg.Context = "";
}
else smsg.Context = Encryption.Generic.Encoders[(int)smsg.EncoderType]
.GetString(Convert.FromBase64String(smsg.Context));
}
else
{
LocalKeyInfo key = EncryptionHandler.GetKey(smsg.EncryptionKey);
switch (key.EncryptionType)
{
case EncryptionType.RSA:
smsg.Context = Encryption.RSA.Decrypt(Convert.FromBase64String(smsg.Context), key.Key,
smsg.EncoderType);
break;
default:
smsg.Context = Encryption.Generic.Encoders[(int)smsg.EncoderType]
.GetString(Convert.FromBase64String(smsg.Context));
break;
}
}
SocketMessage sm = new()
{
ID = smsg.ID,
AuthorID = smsg.AuthorID,
2024-08-27 10:57:22 -04:00
TimeStamp = smsg.Timestamp,
2024-03-20 23:18:34 -04:00
ChannelID = smsg.ChannelID,
Context = smsg.Context,
EncoderType = smsg.EncoderType,
EncryptionKey = smsg.EncryptionKey,
FileIDs = fl.ToArray(),
Server = this,
2024-08-27 10:57:22 -04:00
ProfileID = smsg.ProfileID,
_Files = sf
2024-03-20 23:18:34 -04:00
};
if (MessageReceived is not null) MessageReceived.Invoke(sm);
break;
default:
Console.WriteLine("Unknown");
break;
}
}
catch (Exception exception)
{
Exception(exception);
}
}
}