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); }
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); } }
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); } }
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); }
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))); }
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); }
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); }
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); } } }
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); } } }
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)); }
public void Broadcast(SockChatUser user, string message, SockChatMessageFlags flags = SockChatMessageFlags.RegularUser) { lock (Users) Users.ForEach(u => u.Send(user, message, flags)); }
public SockChatChannel FindUserChannel(SockChatUser user) { return(Channels.FirstOrDefault(c => c.Users.Contains(user))); }
public bool HasUser(SockChatUser user) => Users.Contains(user);