Example #1
0
        public void HandlePacket(PacketHandlerContext 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;
            }

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

            if (string.IsNullOrWhiteSpace(channelName))
            {
                ChannelContinue(ctx, ctx.Connection.LastChannel, text); // this should grab from the user, not the context wtf
            }
            else
            {
                Channels.GetChannelByName(channelName, channel => ChannelContinue(ctx, channel, text)); // this also doesn't check if we're actually in the channel
            }
        }
        public void HandlePacket(PacketHandlerContext 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;
                    }
                }
            }

            ctx.Connection.SendPacket(new CapabilityConfirmationPacket(caps));
            ctx.Connection.Capabilities = caps;
        }
Example #3
0
        private void ChannelContinue(PacketHandlerContext ctx, IChannel channel, string text)
        {
            if (channel == null
                //  || !ChannelUsers.HasUser(channel, ctx.User) look below
                //  || (ctx.User.IsSilenced && !ctx.User.Can(UserPermissions.SilenceUser)) TODO: readd silencing
                )
            {
                return;
            }

            ChannelUsers.HasSession(channel, ctx.Session, hasSession => {
                if (!hasSession)
                {
                    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, ctx.Connection);
                    } catch (CommandException ex) {
                        ctx.Connection.SendPacket(ex.ToPacket(Bot));
                        handled = true;
                    }
                }

                if (!handled)
                {
                    Messages.Create(ctx.Session, channel, text);
                }
            });
        }
Example #4
0
        public void HandlePacket(PacketHandlerContext 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}] Auth fail: {ex.Message}");
                ctx.Connection.SendPacket(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.SendPacket(new AuthFailPacket(AuthFailReason.Banned, ban));
                        ctx.Connection.Close();
                        return;
                    }

                    Users.Connect(res, user => {
                        Sessions.HasAvailableSessions(user, available => {
                            if (!available)
                            {
                                ctx.Connection.SendPacket(new AuthFailPacket(AuthFailReason.MaxSessions));
                                ctx.Connection.Close();
                                return;
                            }

                            Sessions.Create(ctx.Connection, user, session => {
                                string welcome = Server.WelcomeMessage;
                                if (!string.IsNullOrWhiteSpace(welcome))
                                {
                                    ctx.Connection.SendPacket(new WelcomeMessagePacket(Sender, welcome.Replace(@"{username}", user.UserName)));
                                }

                                if (WelcomeMessage.HasRandom)
                                {
                                    string line = WelcomeMessage.GetRandomString();
                                    if (!string.IsNullOrWhiteSpace(line))
                                    {
                                        ctx.Connection.SendPacket(new WelcomeMessagePacket(Sender, line));
                                    }
                                }

                                Channels.GetDefaultChannels(channels => {
                                    if (!channels.Any())
                                    {
                                        return;     // what do, this is impossible
                                    }
                                    // other channels should be joined if MCHAN has been received
                                    IChannel firstChan = channels.FirstOrDefault();

                                    ctx.Connection.LastChannel = firstChan;
                                    ctx.Connection.SendPacket(new AuthSuccessPacket(user, firstChan, session, Messages.TextMaxLength));

                                    Channels.GetChannels(user.Rank, c => ctx.Connection.SendPacket(new ContextChannelsPacket(c)));
                                    ChannelUsers.JoinChannel(firstChan, ctx.Session);
                                });
                            });
                        });
                    });
                }, exceptionHandler);
            },
                exceptionHandler
                );
        }