Esempio n. 1
0
        private void WhoChannel(ICommandContext ctx, string channelName)
        {
            IChannel channel = Channels.GetChannel(channelName);

            if (channel == null)
            {
                throw new ChannelNotFoundCommandException(channelName);
            }

            if (channel.MinimumRank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Can(UserPermissions.JoinAnyChannel)))
            {
                throw new UserListChannelNotFoundCommandException(channelName);
            }

            ChannelUsers.GetUsers(
                channel,
                users => ctx.Session.SendPacket(new UserListResponsePacket(
                                                    Sender,
                                                    channel,
                                                    ctx.User,
                                                    users.OrderByDescending(u => u.Rank)
                                                    ))
                );
        }
Esempio n. 2
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
                );
        }