示例#1
0
        public void BanUser(SockChatUser user, DateTimeOffset?until = null, bool banIPs = false, string type = Constants.LEAVE_KICK)
        {
            if (until.HasValue && until.Value <= DateTimeOffset.UtcNow)
            {
                until = null;
            }

            if (until.HasValue)
            {
                user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Banned, until.Value));
                user.BannedUntil = until.Value;

                if (banIPs)
                {
                    lock (user.Connections)
                        foreach (string ip in user.RemoteAddresses)
                        {
                            if (IPAddress.TryParse(ip, out IPAddress ipAddr))
                            {
                                IPBans[ipAddr] = until.Value;
                            }
                        }
                }
            }
            else
            {
                user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Kicked));
            }

            user.Close();
            UserLeave(user.Channel, user, type);
        }
示例#2
0
        public void ForceChannelSwitch(SockChatUser user, SockChatChannel chan)
        {
            if (!Channels.Contains(chan))
            {
                return;
            }

            int             messageId = SockChatMessage.NextMessageId;
            SockChatChannel oldChan   = user.Channel;

            oldChan.Send(new UserChannelLeavePacket(user), messageId);
            chan.Send(new UserChannelJoinPacket(user), messageId);

            user.Send(new ContextClearPacket(ContextClearMode.MessagesUsers));
            user.Send(new ContextUsersPacket(chan.GetUsers(new[] { user })));

            IChatMessage[] msgs = GetChannelBacklog(chan);

            foreach (IChatMessage msg in msgs)
            {
                user.Send(new ContextMessagePacket(msg));
            }

            user.ForceChannel(chan);
            oldChan.UserLeave(user);
            chan.UserJoin(user);

            if (oldChan.IsTemporary && oldChan.Owner == user)
            {
                DeleteChannel(oldChan);
            }
        }
示例#3
0
        public void HandleJoin(SockChatUser user, SockChatChannel chan, SockChatConn conn)
        {
            if (!chan.HasUser(user))
            {
                chan.Send(new UserConnectPacket(DateTimeOffset.Now, user), SockChatMessage.NextMessageId);
            }

            conn.Send(new AuthSuccessPacket(user, chan));
            conn.Send(new ContextUsersPacket(chan.GetUsers(new[] { user })));

            IChatMessage[] msgs = GetChannelBacklog(chan);

            foreach (IChatMessage msg in msgs)
            {
                conn.Send(new ContextMessagePacket(msg));
            }

            lock (Channels)
                conn.Send(new ContextChannelsPacket(Channels.Where(x => user.Hierarchy >= x.Hierarchy)));

            if (!chan.HasUser(user))
            {
                chan.UserJoin(user);
            }

            if (!Users.Contains(user))
            {
                Users.Add(user);
            }
        }
示例#4
0
        public void SwitchChannel(SockChatUser user, SockChatChannel chan, string password)
        {
            if (user.Channel == chan)
            {
                //user.Send(true, @"samechan", chan.Name);
                user.ForceChannel();
                return;
            }

            if (!user.IsModerator && chan.Owner != user)
            {
                if (chan.Hierarchy > user.Hierarchy)
                {
                    user.Send(true, @"ipchan", chan.Name);
                    user.ForceChannel();
                    return;
                }

                if (chan.Password != password)
                {
                    user.Send(true, @"ipwchan", chan.Name);
                    user.ForceChannel();
                    return;
                }
            }

            ForceChannelSwitch(user, chan);
        }
示例#5
0
        public void Send(SockChatUser user, string message, SockChatMessageFlags flags = SockChatMessageFlags.RegularUser)
        {
            user = user ?? SockChatServer.Bot;

            Connections.ForEach(c => c.Send(new object[] {
                DateTimeOffset.Now.ToUnixTimeSeconds().ToString(), user.UserId.ToString(),
                message, SockChatMessage.NextMessageId,
                flags.Serialise()
            }.Pack(SockChatServerPacket.MessageAdd)));
        }
示例#6
0
        public void SwitchChannel(SockChatUser user, string chanName, string password)
        {
            SockChatChannel chan = FindChannelByName(chanName);

            if (chan == null)
            {
                user.Send(true, @"nochan", chanName);
                user.ForceChannel();
                return;
            }

            SwitchChannel(user, chan, password);
        }
示例#7
0
        public void UserLeave(SockChatChannel chan, SockChatUser user, string type = Constants.LEAVE_NORMAL)
        {
            if (chan == null)
            {
                Channels.Where(x => x.Users.Contains(user)).ToList().ForEach(x => UserLeave(x, user, type));
                return;
            }

            if (chan.IsTemporary && chan.Owner == user)
            {
                DeleteChannel(chan);
            }

            chan.UserLeave(user);
            HandleLeave(chan, user, type);
        }
示例#8
0
        public void UserLeave(SockChatUser user)
        {
            lock (Users)
            {
                lock (user.Channels)
                    if (user.Channels.Contains(this))
                    {
                        user.Channels.Remove(this);
                    }

                if (Users.Contains(user))
                {
                    Users.Remove(user);
                }
            }
        }
示例#9
0
        public void UserJoin(SockChatUser user)
        {
            lock (Users)
            {
                lock (user.Channels)
                    if (!user.Channels.Contains(this))
                    {
                        user.Channel?.UserLeave(user);
                        user.Channels.Add(this);
                    }

                if (!Users.Contains(user))
                {
                    Users.Add(user);
                }
            }
        }
示例#10
0
        public void HandleLeave(SockChatChannel chan, SockChatUser user, string type = Constants.LEAVE_NORMAL)
        {
            UserDisconnectReason reason = UserDisconnectReason.Leave;

            switch (type)
            {
            case Constants.LEAVE_TIMEOUT:
                reason = UserDisconnectReason.TimeOut;
                break;

            case Constants.LEAVE_KICK:
                reason = UserDisconnectReason.Kicked;
                break;

            case Constants.LEAVE_FLOOD:
                reason = UserDisconnectReason.Flood;
                break;
            }

            chan.Send(new UserDisconnectPacket(DateTimeOffset.Now, user, reason));
        }
示例#11
0
 public void Broadcast(SockChatUser user, string message, SockChatMessageFlags flags = SockChatMessageFlags.RegularUser)
 {
     lock (Users)
         Users.ForEach(u => u.Send(user, message, flags));
 }
示例#12
0
 public SockChatChannel FindUserChannel(SockChatUser user)
 {
     return(Channels.FirstOrDefault(c => c.Users.Contains(user)));
 }
示例#13
0
 public bool HasUser(SockChatUser user)
 => Users.Contains(user);