347 lines
8.8 KiB
C#
347 lines
8.8 KiB
C#
|
|
||
|
using Newtonsoft.Json;
|
||
|
using Newtonsoft.Json.Linq;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System;
|
||
|
using System.Net.Http;
|
||
|
using System.Text;
|
||
|
|
||
|
|
||
|
namespace Chatr
|
||
|
{
|
||
|
internal class appcfg
|
||
|
{
|
||
|
public bool WS_SAME_AS_HTTP {get;set;}
|
||
|
public ushort WSPort {get;set;}
|
||
|
public bool WSS {get;set;}
|
||
|
|
||
|
}
|
||
|
public class State
|
||
|
{
|
||
|
|
||
|
public string Email {get;set;}
|
||
|
public string Name {get;set;}
|
||
|
public string Url {get;set;}
|
||
|
public bool Loggedin {get;set;}
|
||
|
|
||
|
public List<MyMessage> Mymessages {get;set;}
|
||
|
|
||
|
public List<UserAccount> Users {get;set;}
|
||
|
}
|
||
|
public class ChatrWSMsg
|
||
|
{
|
||
|
public ChatrWSMsg()
|
||
|
{
|
||
|
PacketType=PacketType.NoOp;
|
||
|
|
||
|
}
|
||
|
public ChatrWSMsg(IPacketData data)
|
||
|
{
|
||
|
SetPacketData(data);
|
||
|
}
|
||
|
public PacketType PacketType {get;set;}
|
||
|
public JObject Data {get;set;}
|
||
|
|
||
|
public IPacketData GetPacketData()
|
||
|
{
|
||
|
if(Data == null) return new NoOpPacket();
|
||
|
switch(PacketType)
|
||
|
{
|
||
|
case PacketType.AuthenticateUser:
|
||
|
return Data.ToObject<AuthenticationUserPacket>();
|
||
|
case PacketType.AuthenticateBot:
|
||
|
return Data.ToObject<AuthenticateBotPacket>();
|
||
|
case PacketType.Message:
|
||
|
return Data.ToObject<MessagePacket>();
|
||
|
case PacketType.Notification:
|
||
|
return Data.ToObject<NotificationPacket>();
|
||
|
case PacketType.NotificationEventFire:
|
||
|
return Data.ToObject<NotificationEventFirePacket>();
|
||
|
case PacketType.ClearAll:
|
||
|
return Data.ToObject<ClearAllPacket>();
|
||
|
}
|
||
|
|
||
|
return new NoOpPacket();
|
||
|
}
|
||
|
public void SetPacketData(IPacketData data)
|
||
|
{
|
||
|
PacketType=data.PacketType;
|
||
|
Data=JObject.FromObject(data);
|
||
|
}
|
||
|
|
||
|
public bool IsType(PacketType type,out IPacketData data)
|
||
|
{
|
||
|
|
||
|
var res= type == PacketType;
|
||
|
if(res)
|
||
|
{
|
||
|
var gata=GetPacketData();
|
||
|
if(gata != null)
|
||
|
{
|
||
|
data=gata;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
data=new NoOpPacket();
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public abstract class IPacketData
|
||
|
{
|
||
|
|
||
|
public abstract PacketType PacketType {get;}
|
||
|
}
|
||
|
public class NoOpPacket : IPacketData
|
||
|
{
|
||
|
public override PacketType PacketType => PacketType.NoOp;
|
||
|
}
|
||
|
public class AuthenticationUserPacket : IPacketData
|
||
|
{
|
||
|
public AuthenticationUserPacket()
|
||
|
{
|
||
|
SessionId="";
|
||
|
}
|
||
|
[JsonIgnore]
|
||
|
public override PacketType PacketType => PacketType.AuthenticateUser;
|
||
|
|
||
|
public string SessionId {get;set;}
|
||
|
}
|
||
|
public class AuthenticateBotPacket : IPacketData
|
||
|
{
|
||
|
public AuthenticateBotPacket()
|
||
|
{
|
||
|
ApiKey="";
|
||
|
}
|
||
|
[JsonIgnore]
|
||
|
public override PacketType PacketType => PacketType.AuthenticateBot;
|
||
|
|
||
|
|
||
|
public string ApiKey {get;set;}
|
||
|
}
|
||
|
public class ClearAllPacket : IPacketData
|
||
|
{
|
||
|
public override PacketType PacketType => PacketType.ClearAll;
|
||
|
|
||
|
public string UserStr {get;set;}
|
||
|
}
|
||
|
public class MessagePacket : IPacketData
|
||
|
{
|
||
|
public override PacketType PacketType => PacketType.Message;
|
||
|
|
||
|
public string Name {get;set;}
|
||
|
public string Content {get;set;}
|
||
|
public string CreationTime {get;set;}
|
||
|
public string UserStr {get;set;}
|
||
|
}
|
||
|
public enum PacketType
|
||
|
{
|
||
|
NoOp=0,
|
||
|
AuthenticateUser=1,
|
||
|
AuthenticateBot=2,
|
||
|
Message=3,
|
||
|
|
||
|
Notification=4,
|
||
|
|
||
|
NotificationEventFire=5,
|
||
|
ClearAll= 6
|
||
|
|
||
|
}
|
||
|
public class NotificationEventFirePacket : IPacketData
|
||
|
{
|
||
|
public string DestinationUserName {get;set;}
|
||
|
public string SourceUserName {get;set;}
|
||
|
public string SourceBotUserName {get;set;}
|
||
|
public string BotNotificationId {get;set;}
|
||
|
public string Button {get;set;}
|
||
|
public string BotUserString {get;set;}
|
||
|
public override PacketType PacketType => PacketType.NotificationEventFire;
|
||
|
}
|
||
|
public class NotificationPacket : IPacketData
|
||
|
{
|
||
|
public NotificationPacket()
|
||
|
{
|
||
|
OnClick=new NotificationEvent();
|
||
|
NotificationButtons=new List<NotificationButton>();
|
||
|
}
|
||
|
public override PacketType PacketType => PacketType.Notification;
|
||
|
|
||
|
public long Id {get;set;}
|
||
|
|
||
|
public string BotNotificationId {get;set;} //an id chosen by bot
|
||
|
public string BotOwnerName {get;set;} //cleartext name of bot owner
|
||
|
public string BotOwnerUserName {get;set;} //username of bot owner
|
||
|
public string Title {get;set;}
|
||
|
|
||
|
public string Body {get;set;}
|
||
|
|
||
|
public string BotName {get;set;}
|
||
|
public string BotUserString {get;set;}
|
||
|
|
||
|
public string UserName {get;set;}
|
||
|
|
||
|
public NotificationEvent OnClick {get;set;}
|
||
|
|
||
|
public List<NotificationButton> NotificationButtons {get;set;}
|
||
|
|
||
|
}
|
||
|
public class NotificationButton
|
||
|
{
|
||
|
public NotificationButton()
|
||
|
{
|
||
|
Text="";
|
||
|
Event=new NotificationEvent();
|
||
|
}
|
||
|
public string Text {get;set;}
|
||
|
public NotificationEvent Event {get;set;}
|
||
|
|
||
|
public static NotificationButton CreateButton(string text,NotificationEvent evt)
|
||
|
{
|
||
|
NotificationButton btn=new NotificationButton();
|
||
|
btn.Text=text;
|
||
|
btn.Event = evt;
|
||
|
return btn;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public enum NotificationEventType
|
||
|
{
|
||
|
DoNothing=0,
|
||
|
DownloadOnClient=1,
|
||
|
GoToOnClient=2,
|
||
|
CallApiFromServer=3, //is the only one that device has to send for
|
||
|
SendCallbackToBot=4 //ws only
|
||
|
}
|
||
|
public class NotificationEvent
|
||
|
{
|
||
|
public NotificationEvent()
|
||
|
{
|
||
|
Type=NotificationEventType.DoNothing;
|
||
|
Data=new Dictionary<string, string>();
|
||
|
}
|
||
|
|
||
|
public NotificationEventType Type {get;set;}
|
||
|
|
||
|
public Dictionary<string,string> Data {get;set;}
|
||
|
|
||
|
public static NotificationEvent CreateRedirect(string url)
|
||
|
{
|
||
|
NotificationEvent evt=new NotificationEvent();
|
||
|
evt.Type = NotificationEventType.GoToOnClient;
|
||
|
evt.Data.Add("Url",url);
|
||
|
return evt;
|
||
|
}
|
||
|
public static NotificationEvent CreateDownload(string url)
|
||
|
{
|
||
|
NotificationEvent evt=new NotificationEvent();
|
||
|
evt.Type = NotificationEventType.DownloadOnClient;
|
||
|
evt.Data.Add("Url",url);
|
||
|
return evt;
|
||
|
}
|
||
|
public static NotificationEvent CreateBotCallback()
|
||
|
{
|
||
|
NotificationEvent evt=new NotificationEvent();
|
||
|
evt.Type=NotificationEventType.SendCallbackToBot;
|
||
|
return evt;
|
||
|
}
|
||
|
|
||
|
public static NotificationHttpBuilder CreateServerCallback(HttpMethod method,string url)
|
||
|
{
|
||
|
NotificationHttpBuilder builder=new NotificationHttpBuilder(method,url);
|
||
|
return builder;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class NotificationHttpBuilder
|
||
|
{
|
||
|
internal NotificationHttpBuilder(HttpMethod method,string url)
|
||
|
{
|
||
|
this.method = method.Method;
|
||
|
this.url = url;
|
||
|
}
|
||
|
string method;
|
||
|
string url;
|
||
|
Dictionary<string,List<string>> headers=new Dictionary<string, List<string>>();
|
||
|
|
||
|
byte[] body=new byte[0];
|
||
|
private void AddTo(Dictionary<string,List<string>> ls,string key,string value)
|
||
|
{
|
||
|
if(!ls.ContainsKey(key))
|
||
|
{
|
||
|
ls.Add(key,new List<string>());
|
||
|
}
|
||
|
ls[key].Add(value);
|
||
|
}
|
||
|
public NotificationHttpBuilder AddHeader(string key,string value)
|
||
|
{
|
||
|
AddTo(headers,key,value);
|
||
|
return this;
|
||
|
}
|
||
|
public NotificationHttpBuilder SetBody(string data,Encoding encoding)
|
||
|
{
|
||
|
body=encoding.GetBytes(data);
|
||
|
return this;
|
||
|
}
|
||
|
public NotificationHttpBuilder SetBody(string data)
|
||
|
{
|
||
|
return SetBody(data,Encoding.UTF8);
|
||
|
}
|
||
|
public NotificationHttpBuilder SetBody(byte[] data)
|
||
|
{
|
||
|
body = data;
|
||
|
return this;
|
||
|
}
|
||
|
public NotificationEvent Build()
|
||
|
{
|
||
|
NotificationEvent evt=new NotificationEvent();
|
||
|
evt.Type=NotificationEventType.CallApiFromServer;
|
||
|
foreach(var header in headers)
|
||
|
{
|
||
|
int i=0;
|
||
|
foreach(var val in header.Value)
|
||
|
{
|
||
|
evt.Data.Add($"HDR{header.Key}:{i++}",val);
|
||
|
}
|
||
|
}
|
||
|
evt.Data.Add("URL",url);
|
||
|
evt.Data.Add("MET",method);
|
||
|
evt.Data.Add("BDY",Convert.ToBase64String(body));
|
||
|
return evt;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class UserAccount
|
||
|
{
|
||
|
public UserAccount()
|
||
|
{
|
||
|
Messages=new List<MyMessage>();
|
||
|
Hash="";
|
||
|
Text="";
|
||
|
Shown="";
|
||
|
}
|
||
|
public string Shown {get;set;}
|
||
|
public string Hash {get;set;}
|
||
|
public string Text {get;set;}
|
||
|
|
||
|
[JsonIgnore]
|
||
|
public long UId {get;set;}
|
||
|
|
||
|
public List<MyMessage> Messages {get;set;}
|
||
|
}
|
||
|
|
||
|
public class MyMessage
|
||
|
{
|
||
|
public MyMessage()
|
||
|
{
|
||
|
Body = "";
|
||
|
Time="";
|
||
|
}
|
||
|
public string Time {get;set;}
|
||
|
public bool Mine {get;set;}
|
||
|
public string Body {get;set;}
|
||
|
|
||
|
}
|
||
|
}
|