Пример #1
0
        protected virtual bool AddChatUser(ChatUserExtension user)
        {
            // Add the new user
            ChatUsers[user.Username.ToLower()] = user;

            user.Peer.Disconnected += OnClientDisconnected;

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Handles chat message.
        /// Returns true, if message was handled
        /// If it returns false, message sender will receive a "Not Handled" response.
        /// </summary>
        protected virtual bool OnChatMessageReceived(ChatMessagePacket message,
                                                     ChatUserExtension sender, IIncommingMessage rawMessage)
        {
            // Set a true sender
            message.Sender = sender.Username;

            switch (message.Type)
            {
            case ChatMessagePacket.ChannelMessage:

                if (string.IsNullOrEmpty(message.Receiver))
                {
                    // If this is a local chat message (no receiver is provided)
                    if (sender.DefaultChannel == null)
                    {
                        rawMessage.Respond("No channel is set to be your local channel", ResponseStatus.Failed);
                        return(true);
                    }

                    sender.DefaultChannel.BroadcastMessage(message);
                    rawMessage.Respond(ResponseStatus.Success);
                    return(true);
                }

                // Find the channel
                Channels.TryGetValue(message.Receiver.ToLower(), out var channel);

                if (channel == null || !sender.CurrentChannels.Contains(channel))
                {
                    // Not in this channel
                    rawMessage.Respond($"You're not in the '{message.Receiver}' channel",
                                       ResponseStatus.Failed);
                    return(true);
                }

                channel.BroadcastMessage(message);

                rawMessage.Respond(ResponseStatus.Success);
                return(true);

            case ChatMessagePacket.PrivateMessage:
                ChatUsers.TryGetValue(message.Receiver.ToLower(), out var receiver);

                if (receiver == null)
                {
                    rawMessage.Respond($"User '{message.Receiver}' is not online", ResponseStatus.Failed);
                    return(true);
                }

                receiver.Peer.SendMessage((short)OpCodes.ChatMessage, message);
                rawMessage.Respond(ResponseStatus.Success);
                return(true);
            }

            return(false);
        }
Пример #3
0
        protected virtual void RemoveChatUser(ChatUserExtension user)
        {
            ChatUsers.Remove(user.Username.ToLower());

            var channels = user.CurrentChannels.ToList();

            foreach (var chatChannel in channels)
            {
                chatChannel.RemoveUser(user);
            }

            user.Peer.Disconnected -= OnClientDisconnected;
        }
Пример #4
0
        protected virtual void OnLeft(ChatUserExtension removedUser)
        {
            var data = new List <string>()
            {
                Name, removedUser.Username
            };
            var msg = MessageHelper.Create((short)OpCodes.UserLeftChannel, data.ToBytes());

            foreach (var user in _users.Values)
            {
                if (user != removedUser)
                {
                    user.Peer.SendMessage(msg, DeliveryMethod.ReliableOrdered);
                }
            }
        }
Пример #5
0
        public void RemoveUser(ChatUserExtension user)
        {
            // Remove disconnect listener
            user.Peer.Disconnected -= OnUserDisconnect;

            // Remove channel from users collection
            user.CurrentChannels.Remove(this);

            // Remove user
            _users.Remove(user.Username);

            if (user.DefaultChannel == this)
            {
                user.DefaultChannel = null;
            }

            OnLeft(user);
        }
Пример #6
0
        /// <summary>
        /// Returns true, if user successfully joined a channel
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUser(ChatUserExtension user)
        {
            if (!IsUserAllowed(user))
            {
                return(false);
            }

            // Add disconnect listener
            user.Peer.Disconnected += OnUserDisconnect;

            // Add user
            _users.Add(user.Username, user);

            // Add channel to users collection
            user.CurrentChannels.Add(this);

            OnJoined(user);
            return(true);
        }
Пример #7
0
        protected virtual void HandlePickUsername(IIncommingMessage message)
        {
            if (!AllowUsernamePicking)
            {
                message.Respond("Username picking is disabled", ResponseStatus.Failed);
                return;
            }

            var username = message.AsString();

            if (username.Replace(" ", "") != username)
            {
                message.Respond("Username cannot contain whitespaces", ResponseStatus.Failed);
                return;
            }

            var chatUser = message.Peer.GetExtension <ChatUserExtension>();

            if (chatUser != null)
            {
                message.Respond("You're already identified as: " + chatUser);
                return;
            }

            if (ChatUsers.ContainsKey(username.ToLower()))
            {
                message.Respond("There's already a user who has the same username", ResponseStatus.Failed);
                return;
            }

            chatUser = new ChatUserExtension(message.Peer, username);

            if (!AddChatUser(chatUser))
            {
                message.Respond("Failed to add user to chat", ResponseStatus.Failed);
                return;
            }

            // Add the extension
            message.Peer.AddExtension(chatUser);

            message.Respond(ResponseStatus.Success);
        }
Пример #8
0
 protected virtual bool IsUserAllowed(ChatUserExtension user)
 {
     // Can't join if already here
     return(!_users.ContainsKey(user.Username));
 }