2023-02-16 20:34:59 +00:00
using Fleck ;
using SharpChat.Events ;
2023-02-10 06:07:59 +00:00
using SharpChat.EventStorage ;
2022-08-30 15:00:58 +00:00
using SharpChat.Packet ;
using System ;
using System.Collections.Generic ;
2023-02-10 06:07:59 +00:00
using System.Linq ;
2022-08-30 15:00:58 +00:00
namespace SharpChat {
2023-02-10 06:07:59 +00:00
public class ChatContext {
public HashSet < ChatChannel > Channels { get ; } = new ( ) ;
public readonly object ChannelsAccess = new ( ) ;
2022-08-30 15:00:58 +00:00
2023-02-16 21:25:41 +00:00
public HashSet < ChatConnection > Connections { get ; } = new ( ) ;
public readonly object ConnectionsAccess = new ( ) ;
2023-02-16 20:34:59 +00:00
2023-02-10 06:07:59 +00:00
public HashSet < ChatUser > Users { get ; } = new ( ) ;
public readonly object UsersAccess = new ( ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
public IEventStorage Events { get ; }
public readonly object EventsAccess = new ( ) ;
public ChatContext ( IEventStorage evtStore ) {
Events = evtStore ? ? throw new ArgumentNullException ( nameof ( evtStore ) ) ;
2022-08-30 15:00:58 +00:00
}
public void Update ( ) {
2023-02-10 06:07:59 +00:00
lock ( UsersAccess )
foreach ( ChatUser user in Users ) {
2023-02-16 21:25:41 +00:00
IEnumerable < ChatConnection > timedOut = user . GetDeadConnections ( ) ;
2023-02-10 06:07:59 +00:00
2023-02-16 21:25:41 +00:00
foreach ( ChatConnection conn in timedOut ) {
user . RemoveConnection ( conn ) ;
conn . Dispose ( ) ;
Logger . Write ( $"Nuked session {conn.Id} from {user.Username} (timeout)" ) ;
2023-02-10 06:07:59 +00:00
}
2023-02-16 21:25:41 +00:00
if ( ! user . HasConnections )
2023-02-10 06:07:59 +00:00
UserLeave ( null , user , UserDisconnectReason . TimeOut ) ;
}
2022-08-30 15:00:58 +00:00
}
2023-02-16 21:25:41 +00:00
public ChatConnection GetConnection ( IWebSocketConnection sock ) {
return Connections . FirstOrDefault ( s = > s . Socket = = sock ) ;
2023-02-16 20:34:59 +00:00
}
2023-02-07 22:28:06 +00:00
public void BanUser ( ChatUser user , TimeSpan duration , UserDisconnectReason reason = UserDisconnectReason . Kicked ) {
if ( duration > TimeSpan . Zero )
user . Send ( new ForceDisconnectPacket ( ForceDisconnectReason . Banned , DateTimeOffset . Now + duration ) ) ;
else
2022-08-30 15:00:58 +00:00
user . Send ( new ForceDisconnectPacket ( ForceDisconnectReason . Kicked ) ) ;
user . Close ( ) ;
UserLeave ( user . Channel , user , reason ) ;
}
2023-02-16 21:25:41 +00:00
public void HandleJoin ( ChatUser user , ChatChannel chan , ChatConnection conn , int maxMsgLength ) {
2023-02-10 06:07:59 +00:00
lock ( EventsAccess ) {
if ( ! chan . HasUser ( user ) ) {
chan . Send ( new UserConnectPacket ( DateTimeOffset . Now , user ) ) ;
Events . AddEvent ( new UserConnectEvent ( DateTimeOffset . Now , user , chan ) ) ;
}
2022-08-30 15:00:58 +00:00
2023-02-16 21:25:41 +00:00
conn . Send ( new AuthSuccessPacket ( user , chan , conn , maxMsgLength ) ) ;
conn . Send ( new ContextUsersPacket ( chan . GetUsers ( new [ ] { user } ) ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
foreach ( IChatEvent msg in Events . GetTargetEventLog ( chan . Name ) )
2023-02-16 21:25:41 +00:00
conn . Send ( new ContextMessagePacket ( msg ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
lock ( ChannelsAccess )
2023-02-16 21:25:41 +00:00
conn . Send ( new ContextChannelsPacket ( Channels . Where ( c = > c . Rank < = user . Rank ) ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
if ( ! chan . HasUser ( user ) )
chan . UserJoin ( user ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
lock ( UsersAccess )
Users . Add ( user ) ;
}
2022-08-30 15:00:58 +00:00
}
public void UserLeave ( ChatChannel chan , ChatUser user , UserDisconnectReason reason = UserDisconnectReason . Leave ) {
user . Status = ChatUserStatus . Offline ;
2023-02-07 15:01:56 +00:00
if ( chan = = null ) {
2022-08-30 15:00:58 +00:00
foreach ( ChatChannel channel in user . GetChannels ( ) ) {
UserLeave ( channel , user , reason ) ;
}
return ;
}
2023-02-07 15:01:56 +00:00
if ( chan . IsTemporary & & chan . Owner = = user )
2023-02-10 06:07:59 +00:00
lock ( ChannelsAccess )
RemoveChannel ( chan ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
lock ( EventsAccess ) {
chan . UserLeave ( user ) ;
chan . Send ( new UserDisconnectPacket ( DateTimeOffset . Now , user , reason ) ) ;
Events . AddEvent ( new UserDisconnectEvent ( DateTimeOffset . Now , user , chan , reason ) ) ;
}
2022-08-30 15:00:58 +00:00
}
public void SwitchChannel ( ChatUser user , ChatChannel chan , string password ) {
2023-02-07 15:01:56 +00:00
if ( user . CurrentChannel = = chan ) {
2023-02-08 03:17:07 +00:00
//user.Send(true, "samechan", chan.Name);
2022-08-30 15:00:58 +00:00
user . ForceChannel ( ) ;
return ;
}
2023-02-07 15:01:56 +00:00
if ( ! user . Can ( ChatUserPermissions . JoinAnyChannel ) & & chan . Owner ! = user ) {
if ( chan . Rank > user . Rank ) {
2022-08-30 15:00:58 +00:00
user . Send ( new LegacyCommandResponse ( LCR . CHANNEL_INSUFFICIENT_HIERARCHY , true , chan . Name ) ) ;
user . ForceChannel ( ) ;
return ;
}
2023-02-07 15:01:56 +00:00
if ( chan . Password ! = password ) {
2022-08-30 15:00:58 +00:00
user . Send ( new LegacyCommandResponse ( LCR . CHANNEL_INVALID_PASSWORD , true , chan . Name ) ) ;
user . ForceChannel ( ) ;
return ;
}
}
ForceChannelSwitch ( user , chan ) ;
}
public void ForceChannelSwitch ( ChatUser user , ChatChannel chan ) {
2023-02-10 06:07:59 +00:00
lock ( ChannelsAccess )
if ( ! Channels . Contains ( chan ) )
return ;
2022-08-30 15:00:58 +00:00
ChatChannel oldChan = user . CurrentChannel ;
2023-02-10 06:07:59 +00:00
lock ( EventsAccess ) {
oldChan . Send ( new UserChannelLeavePacket ( user ) ) ;
Events . AddEvent ( new UserChannelLeaveEvent ( DateTimeOffset . Now , user , oldChan ) ) ;
chan . Send ( new UserChannelJoinPacket ( user ) ) ;
Events . AddEvent ( new UserChannelJoinEvent ( DateTimeOffset . Now , user , chan ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
user . Send ( new ContextClearPacket ( chan , ContextClearMode . MessagesUsers ) ) ;
user . Send ( new ContextUsersPacket ( chan . GetUsers ( new [ ] { user } ) ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
foreach ( IChatEvent msg in Events . GetTargetEventLog ( chan . Name ) )
user . Send ( new ContextMessagePacket ( msg ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
user . ForceChannel ( chan ) ;
oldChan . UserLeave ( user ) ;
chan . UserJoin ( user ) ;
}
2022-08-30 15:00:58 +00:00
2023-02-07 15:01:56 +00:00
if ( oldChan . IsTemporary & & oldChan . Owner = = user )
2023-02-10 06:07:59 +00:00
lock ( ChannelsAccess )
RemoveChannel ( oldChan ) ;
2022-08-30 15:00:58 +00:00
}
2023-02-10 06:07:59 +00:00
public void Send ( IServerPacket packet ) {
lock ( UsersAccess )
foreach ( ChatUser user in Users )
user . Send ( packet ) ;
}
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
public void UpdateChannel ( ChatChannel channel , string name = null , bool? temporary = null , int? hierarchy = null , string password = null ) {
if ( channel = = null )
throw new ArgumentNullException ( nameof ( channel ) ) ;
if ( ! Channels . Contains ( channel ) )
throw new ArgumentException ( "Provided channel is not registered with this manager." , nameof ( channel ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
string prevName = channel . Name ;
int prevHierarchy = channel . Rank ;
bool nameUpdated = ! string . IsNullOrWhiteSpace ( name ) & & name ! = prevName ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
if ( nameUpdated ) {
if ( ! ChatChannel . CheckName ( name ) )
throw new ArgumentException ( "Name contains invalid characters." , nameof ( name ) ) ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
channel . Name = name ;
}
if ( temporary . HasValue )
channel . IsTemporary = temporary . Value ;
if ( hierarchy . HasValue )
channel . Rank = hierarchy . Value ;
if ( password ! = null )
channel . Password = password ;
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
// Users that no longer have access to the channel/gained access to the channel by the hierarchy change should receive delete and create packets respectively
lock ( UsersAccess )
foreach ( ChatUser user in Users . Where ( u = > u . Rank > = channel . Rank ) ) {
user . Send ( new ChannelUpdatePacket ( prevName , channel ) ) ;
if ( nameUpdated )
user . ForceChannel ( ) ;
}
2023-02-06 20:14:50 +00:00
}
2022-08-30 15:00:58 +00:00
2023-02-10 06:07:59 +00:00
public void RemoveChannel ( ChatChannel channel ) {
if ( channel = = null | | ! Channels . Any ( ) )
return ;
ChatChannel defaultChannel = Channels . FirstOrDefault ( ) ;
if ( defaultChannel = = null )
2022-08-30 15:00:58 +00:00
return ;
2023-02-10 06:07:59 +00:00
// Remove channel from the listing
Channels . Remove ( channel ) ;
// Move all users back to the main channel
// TODO: Replace this with a kick. SCv2 supports being in 0 channels, SCv1 should force the user back to DefaultChannel.
foreach ( ChatUser user in channel . GetUsers ( ) )
SwitchChannel ( user , defaultChannel , string . Empty ) ;
// Broadcast deletion of channel
lock ( UsersAccess )
foreach ( ChatUser user in Users . Where ( u = > u . Rank > = channel . Rank ) )
user . Send ( new ChannelDeletePacket ( channel ) ) ;
2022-08-30 15:00:58 +00:00
}
}
}