コード例 #1
0
 private void Wait()
 {
     while (true)
     {
         try {
             FClient = FServer.AcceptTcpClient();
             var Connection        = new Connection(FClient.GetStream());
             var RawStreamContent  = Connection.GetRawStreamContent();
             var DRawStreamContent = Cryptography.RSADecrypt(RawStreamContent, ServiceProvider);
             if (DRawStreamContent == F2CE.Action.GetServerMetaData.ToString())
             {
                 Connection.SetRawStreamContent(Cryptography.RSAEncrypt(JsonConvert.SerializeObject(GetMetaData()), PreServiceProvider));
                 Connection.Dispose();
                 Management.PunishmentManager.DisposeExceededRecords();
             }
             else
             {
                 var SessionData = DRawStreamContent.Split('|');
                 var AesData     = new AesData {
                     Key = Convert.FromBase64String(SessionData[0]),
                     IV  = Convert.FromBase64String(SessionData[1])
                 };
                 Connection.AesData = AesData;
                 Connection.HmacKey = Convert.FromBase64String(SessionData[2]);
                 var AuthData = Connection.GetStreamContent().Split('|');
                 var Account  = Storage.Database.Helper.GetAccount(AuthData[0]);
                 if (Account != null)
                 {
                     if (Account.Password == AuthData[1])
                     {
                         var PunishmentId = Management.PunishmentManager.CheckForRecords(Account.Id, F2CE.PunishmentType.Bann, F2CE.PunishmentType.BannTemporarily);
                         if (PunishmentId != "-1")
                         {
                             var Punishment = Management.PunishmentManager.GetRecord(PunishmentId);
                             Connection.SetStreamContent(string.Join("|", F2CE.Action.SetState.ToString(), F2CE.ClientState.Banned.ToString(), JsonConvert.SerializeObject(Punishment)));
                         }
                         else
                         {
                             Connection.Owner   = Account;
                             Connection.Channel = Forestual;
                             Connections.Add(Connection);
                             var ListeningThread = new Thread(Listen);
                             ListeningThread.Start(Connection);
                             Connection.SetStreamContent(string.Join("|", F2CE.Action.LoginResult.ToString(), "hej"));
                             var Flags = new List <F2CE.Flag>();
                             Flags.AddRange(Connection.Owner.Flags);
                             Flags.AddRange(Database.Ranks.Find(r => r.Id == Connection.Owner.RankId).Flags);
                             Connection.SetStreamContent(string.Join("|", F2CE.Action.SetFlags, JsonConvert.SerializeObject(Flags)));
                             Forestual.MemberIds.Add(Account.Id);
                             Management.ChannelManager.SendChannelList();
                             var Message = new Forestual2Core.Message {
                                 Time    = DateTime.Now.ToShortTimeString(),
                                 Type    = F2CE.MessageType.Center,
                                 Content = $"{Connection.Owner.Name} (@{Connection.Owner.Id}) hat den Chat betreten."
                                           // Demo Purposes
                             };
                             SendMessageToAll(Message);
                             Connection.Owner.Online = true;
                             SendToAll(string.Join("|", F2CE.Action.SetAccountList, JsonConvert.SerializeObject(GetAccountsWithoutPassword())));
                             PrintToConsole($"{Connection.Owner.Name} (@{Connection.Owner.Id}) joined.", ColorTranslator.FromHtml("#07D159"));
                         }
                     }
                     else
                     {
                         Connection.SetStreamContent(string.Join("|", F2CE.Action.LoginResult, "authentificationFailed"));
                     }
                 }
                 else
                 {
                     Connection.SetStreamContent(string.Join("|", F2CE.Action.LoginResult, "accountUnknown"));
                 }
             }
         } catch { }
     }
 }
コード例 #2
0
 private void SendMessageToAllExceptTo(string accountId, Forestual2Core.Message message, string channelId)
 {
     Connections.FindAll(c => c.Owner.Id != accountId && c.Channel.Id == channelId).ForEach(c => c.SetStreamContent(string.Join("|", F2CE.Action.Plain, JsonConvert.SerializeObject(message))));
 }
コード例 #3
0
 private void SendMessageTo(string id, Forestual2Core.Message message)
 {
     Connections.Find(c => c.Owner.Id == id).SetStreamContent(string.Join("|", F2CE.Action.Plain, JsonConvert.SerializeObject(message)));
 }
コード例 #4
0
 private void SendMessageToAll(Forestual2Core.Message message)
 {
     Connections.ForEach(c => c.SetStreamContent(string.Join("|", F2CE.Action.Plain, JsonConvert.SerializeObject(message))));
 }
コード例 #5
0
        private void Listen(object o)
        {
            var Connection = (Connection)o;

            while (true)
            {
                if (ExitThreadOnPurpose)
                {
                    return;
                }
                try {
                    var Content      = Connection.GetStreamContent();
                    var PunishmentId = Management.PunishmentManager.CheckForRecords(Connection.Owner.Id, F2CE.PunishmentType.Mute);
                    if (PunishmentId != "-1")
                    {
                        var RemainingTime = Management.PunishmentManager.GetRecord(PunishmentId).EndDate.Subtract(DateTime.Now);
                        var Message       = new Forestual2Core.Message {
                            Time      = DateTime.Now.ToShortTimeString(),
                            Type      = F2CE.MessageType.Center,
                            RankColor = "#FC3539",
                            Content   = $"Your mute still lasts {RemainingTime.Days} days, {RemainingTime.Hours} hours, {RemainingTime.Minutes} minutes and {RemainingTime.Seconds} seconds."
                        };
                        SendMessageTo(Connection.Owner.Id, Message);
                        continue;
                    }
                    try {
                        var Contents = Content.Split('|');
                        if (string.IsNullOrEmpty(Contents[0]) || string.IsNullOrWhiteSpace(Contents[0]))
                        {
                            continue;
                        }
                        var Type = (F2CE.Action)Enum.Parse(typeof(F2CE.Action), Contents[0]);
                        switch (Type)
                        {
                        case F2CE.Action.Plain:
                            if (Contents[1].StartsWith("/"))
                            {
                                var CommandParts = Contents[1].Remove(0, 1).Split(' ');
                                switch (CommandParts[0])
                                {
                                case "clear":
                                    SendToChannel(Connection.Channel.Id, F2CE.Action.ClearConversation.ToString());
                                    break;

                                case "vuohen":
                                    var Account = CommandParts.Length >= 3 ? Storage.Database.Helper.GetAccount(CommandParts[2]) : null;
                                    var Value   = CommandParts.Length >= 4 ? int.Parse(CommandParts[3]) : 0;
                                    switch (CommandParts[1])
                                    {
                                    case "add":
                                        Account.Deposit += Value;
                                        break;

                                    case "remove":
                                        Account.Deposit -= Value;
                                        if (Account.Deposit < 0)
                                        {
                                            Account.Deposit = 0;
                                        }
                                        break;

                                    case "set":
                                        Account.Deposit = Value;
                                        break;

                                    case "send":
                                        if (Connection.Owner.Deposit < Value)
                                        {
                                            var Message1 = new Forestual2Core.Message()
                                            {
                                                Time      = DateTime.Now.ToShortTimeString(),
                                                Type      = F2CE.MessageType.Center,
                                                RankColor = "#FC3539",
                                                Content   = "Your do not have enough indian hillside goats to perform this transaction."
                                            };
                                            SendMessageTo(Connection.Owner.Id, Message1);
                                        }
                                        else
                                        {
                                            Connection.Owner.Deposit -= Value;
                                            Account.Deposit          += Value;
                                        }
                                        break;

                                    case "balance":
                                        var Message2 = new Forestual2Core.Message()
                                        {
                                            Time      = DateTime.Now.ToShortTimeString(),
                                            Type      = F2CE.MessageType.Center,
                                            RankColor = Config.ServerBroadcastColor,
                                            Content   = $"Your balance is {Connection.Owner.Deposit} indian hillside goats."
                                        };
                                        SendMessageTo(Connection.Owner.Id, Message2);
                                        break;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                var Message3 = new Forestual2Core.Message {
                                    SenderId     = Connection.Owner.Id,
                                    SenderPrefix = ComposePrefix(Connection.Owner.Id),
                                    RankColor    = Database.Ranks.Find(r => r.Id == Connection.Owner.RankId).Color,
                                    Time         = DateTime.Now.ToShortTimeString(),
                                    Content      = Contents[1],
                                    Type         = F2CE.MessageType.Left
                                };
                                SendMessageToAllExceptTo(Message3.SenderId, Message3, Connection.Channel.Id);
                                Message3.Type = F2CE.MessageType.Right;
                                SendMessageTo(Message3.SenderId, Message3);
                            }
                            break;

                        case F2CE.Action.RegisterRecord:
                            var Punishment = JsonConvert.DeserializeObject <Punishment>(Contents[1]);
                            Punishment.CreatorId = Connection.Owner.Id;
                            Punishment.Id        = Management.PunishmentManager.GetRandomIdentifier(6);
                            Management.PunishmentManager.CreateRecord(Punishment);
                            break;
                        }
                    } catch {
                        // Syntax Error in Command
                    }
                } catch {
                    Connections.Remove(Connection);
                    var Message = new Forestual2Core.Message {
                        Time    = DateTime.Now.ToShortTimeString(),
                        Type    = F2CE.MessageType.Center,
                        Content = $"{Connection.Owner.Name} disconnected."
                                  // Demo Purposes
                    };
                    SendMessageToAll(Message);
                    Connection.Channel.MemberIds.Remove(Connection.Owner.Id);
                    if (Connection.Channel.Owner == Connection.Owner)
                    {
                        Connection.Channel.Owner = null;
                    }
                    if (Connection.Channel.MemberIds.Count == 0 && !Connection.Channel.Persistent)
                    {
                        Channels.Remove(Connection.Channel);
                        Management.ChannelManager.SendChannelList();
                    }
                    Connection.Owner.Online = false;
                    SendToAll(string.Join("|", F2CE.Action.SetAccountList, JsonConvert.SerializeObject(GetAccountsWithoutPassword())));
                    PrintToConsole($"{Connection.Owner.Name} (@{Connection.Owner.Id}) disconnected.", ColorTranslator.FromHtml("#FC3539"));
                    Connection.Dispose();
                    return;
                }
            }
        }