Exemplo n.º 1
0
        public void HandlePacket(IPacketHandlerContext ctx)
        {
            if (!ctx.HasSession &&
                !long.TryParse(ctx.Args.ElementAtOrDefault(1), out long userId) &&
                ctx.Session.User.UserId != userId)
            {
                return;
            }
            //if(!int.TryParse(ctx.Args.ElementAtOrDefault(2), out int timestamp))
            //    timestamp = -1;

            Sessions.DoKeepAlive(ctx.Session);
        }
Exemplo n.º 2
0
        public void HandlePacket(IPacketHandlerContext ctx)
        {
            if (!ctx.HasSession)
            {
                return;
            }

            ClientCapability caps = 0;

            IEnumerable <string> capStrs = ctx.Args.ElementAtOrDefault(1)?.Split(' ');

            if (capStrs != null && capStrs.Any())
            {
                foreach (string capStr in capStrs)
                {
                    if (Enum.TryParse(typeof(ClientCapability), capStr.ToUpperInvariant(), out object cap))
                    {
                        caps |= (ClientCapability)cap;
                    }
                }
            }

            Sessions.SetCapabilities(ctx.Session, caps);
        }
Exemplo n.º 3
0
 public void HandlePacket(IPacketHandlerContext ctx)
 {
     /*if(!ctx.HasUser)
      *  return;
      *
      * if(!long.TryParse(ctx.Args.ElementAtOrDefault(1), out long userId) || ctx.User.UserId != userId)
      *  return;
      *
      * string channelName = ctx.Args.ElementAtOrDefault(2)?.ToLowerInvariant();
      * if(!string.IsNullOrWhiteSpace(channelName))
      *  return;
      *
      * IChannel channel = ctx.User.GetChannels().FirstOrDefault(c => c.Name.ToLowerInvariant() == channelName);
      * if(channel == null || !channel.CanType(ctx.User))
      *  return;
      *
      * ctx.Session.LastChannel = channel;
      *
      * ChannelTyping info = channel.RegisterTyping(ctx.User);
      * if(info == null)
      *  return;
      *
      * channel.SendPacket(new TypingPacket(channel, info));*/
 }
Exemplo n.º 4
0
        public void HandlePacket(IPacketHandlerContext ctx)
        {
            if (ctx.HasSession)
            {
                return;
            }

            if (!long.TryParse(ctx.Args.ElementAtOrDefault(1), out long userId) || userId < 1)
            {
                return;
            }

            string token = ctx.Args.ElementAtOrDefault(2);

            if (string.IsNullOrEmpty(token))
            {
                return;
            }

            Action <Exception> exceptionHandler = new Action <Exception>(ex => {
                Logger.Debug($@"<{ctx.Connection.RemoteAddress}> Auth fail: {ex.Message}");
                ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
                ctx.Connection.Close();
            });

            DataProvider.UserAuthClient.AttemptAuth(
                new UserAuthRequest(userId, token, ctx.Connection.RemoteAddress),
                res => {
                DataProvider.BanClient.CheckBan(res.UserId, ctx.Connection.RemoteAddress, ban => {
                    if (ban.IsPermanent || ban.Expires > DateTimeOffset.Now)
                    {
                        ctx.Connection.Send(new AuthFailPacket(AuthFailReason.Banned, ban));
                        ctx.Connection.Close();
                        return;
                    }

                    IUser user = Users.Connect(res);

                    // Enforce a maximum amount of connections per user
                    if (Sessions.GetAvailableSessionCount(user) < 1)
                    {
                        ctx.Connection.Send(new AuthFailPacket(AuthFailReason.MaxSessions));
                        ctx.Connection.Close();
                        return;
                    }

                    ISession sess = Sessions.Create(ctx.Connection, user);

                    sess.SendPacket(new WelcomeMessagePacket(Sender, $@"Welcome to Flashii Chat, {user.UserName}!"));

                    if (File.Exists(WELCOME))
                    {
                        IEnumerable <string> lines = File.ReadAllLines(WELCOME).Where(x => !string.IsNullOrWhiteSpace(x));
                        string line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));

                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            sess.SendPacket(new WelcomeMessagePacket(Sender, line));
                        }
                    }

                    IChannel chan   = Channels.DefaultChannel;
                    bool shouldJoin = !ChannelUsers.HasUser(chan, user);

                    if (shouldJoin)
                    {
                        // ChannelUsers?
                        //chan.SendPacket(new UserConnectPacket(DateTimeOffset.Now, user));
                        //ctx.Chat.DispatchEvent(this, new UserConnectEvent(chan, user));
                    }

                    sess.SendPacket(new AuthSuccessPacket(user, chan, sess, Version, Messages.TextMaxLength));
                    ChannelUsers.GetUsers(chan, u => sess.SendPacket(new ContextUsersPacket(u.Except(new[] { user }).OrderByDescending(u => u.Rank))));

                    Messages.GetMessages(chan, m => {
                        foreach (IMessage msg in m)
                        {
                            sess.SendPacket(new ContextMessagePacket(msg));
                        }
                    });

                    Channels.GetChannels(user.Rank, c => sess.SendPacket(new ContextChannelsPacket(c)));

                    if (shouldJoin)
                    {
                        ChannelUsers.JoinChannel(chan, sess);
                    }
                }, exceptionHandler);
            },
                exceptionHandler
                );
        }
Exemplo n.º 5
0
        public void HandlePacket(IPacketHandlerContext ctx)
        {
            if (ctx.Args.Count() < 3 || !ctx.HasUser || !ctx.User.Can(UserPermissions.SendMessage))
            {
                return;
            }

            if (!long.TryParse(ctx.Args.ElementAtOrDefault(1), out long userId) || ctx.User.UserId != userId)
            {
                return;
            }

            // No longer concats everything after index 1 with \t, no previous implementation did that either
            string text = ctx.Args.ElementAtOrDefault(2);

            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            IChannel channel;
            string   channelName = ctx.Args.ElementAtOrDefault(3)?.ToLowerInvariant();

            if (string.IsNullOrWhiteSpace(channelName))
            {
                channel = Channels.DefaultChannel;
            }
            else
            {
                channel = Channels.GetChannel(channelName);
            }

            if (channel == null ||
                !ChannelUsers.HasUser(channel, ctx.User)
                //  || (ctx.User.IsSilenced && !ctx.User.Can(UserPermissions.SilenceUser)) TODO: readd silencing
                )
            {
                return;
            }

            if (ctx.User.Status != UserStatus.Online)
            {
                Users.Update(ctx.User, status: UserStatus.Online);
                // ChannelUsers?
                //channel.SendPacket(new UserUpdatePacket(ctx.User));
            }

            // there's a very miniscule chance that this will return a different value on second read
            int maxLength = Messages.TextMaxLength;

            if (text.Length > maxLength)
            {
                text = text.Substring(0, maxLength);
            }

            text = text.Trim();

#if DEBUG
            Logger.Write($@"<{ctx.Session.SessionId} {ctx.User.UserName}> {text}");
#endif

            bool handled = false;

            if (text[0] == '/')
            {
                try {
                    handled = HandleCommand(text, ctx.User, channel, ctx.Session);
                } catch (CommandException ex) {
                    ctx.Session.SendPacket(ex.ToPacket(Bot));
                    handled = true;
                }
            }

            if (!handled)
            {
                Messages.Create(ctx.User, channel, text);
            }
        }