示例#1
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:
                ChatChannel channel;

                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 channel);

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

                channel.BroadcastMessage(message);

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

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

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

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

            return(false);
        }
示例#2
0
        /// <summary>
        /// Handle messages
        /// </summary>
        public virtual void BroadcastMessage(ChatMessagePacket packet)
        {
            // Override name to be in a "standard" format (uppercase letters and etc.)
            packet.Receiver = Name;

            var msg = Msf.Create.Message((short)MsfOpCodes.ChatMessage, packet);

            foreach (var user in _users.Values)
            {
                user.Peer.SendMessage(msg, DeliveryMethod.Reliable);
            }
        }
示例#3
0
        /// <summary>
        ///     Sends a generic message packet to server
        /// </summary>
        public void SendMessage(ChatMessagePacket packet, SuccessCallback callback, IClientSocket connection)
        {
            connection.SendMessage((short)MsfOpCodes.ChatMessage, packet, (status, response) => {
                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(false, response.AsString("Unknown error"));
                    return;
                }

                callback.Invoke(true, null);
            });
        }
示例#4
0
 /// <summary>
 /// Sends a generic message packet to server
 /// </summary>
 /// <param name="packet"></param>
 /// <param name="callback"></param>
 public void SendMessage(ChatMessagePacket packet, SuccessCallback callback)
 {
     SendMessage(packet, callback, Connection);
 }
示例#5
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 TryHandleChatMessage(ChatMessagePacket message, ChatUserPeerExtension sender, IIncommingMessage rawMessage)
        {
            // Set a true sender
            message.Sender = sender.Username;

            string responseMsg = string.Empty;

            switch (message.MessageType)
            {
            case ChatMessageType.ChannelMessage:

                if (string.IsNullOrEmpty(message.Receiver))
                {
                    // If this is a local chat message (no receiver is provided)
                    if (sender.DefaultChannel == null)
                    {
                        responseMsg = "No channel is set to be your local channel";

                        logger.Debug(responseMsg);

                        rawMessage.Respond(responseMsg, ResponseStatus.Failed);
                        return(true);
                    }

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

                // Find the channel
                if (!ChatChannels.TryGetValue(message.Receiver.ToLower(), out ChatChannel channel) || !sender.CurrentChannels.Contains(channel))
                {
                    responseMsg = $"You're not in the '{message.Receiver}' channel";

                    logger.Error(responseMsg);

                    // Not in this channel
                    rawMessage.Respond(responseMsg, ResponseStatus.Failed);
                    return(true);
                }

                channel.BroadcastMessage(message);

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

            case ChatMessageType.PrivateMessage:

                if (!ChatUsers.TryGetValue(message.Receiver.ToLower(), out ChatUserPeerExtension receiver))
                {
                    responseMsg = $"User '{message.Receiver}' is not online";

                    logger.Error(responseMsg);

                    rawMessage.Respond(responseMsg, ResponseStatus.Failed);
                    return(true);
                }

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

            return(false);
        }