Пример #1
0
        /// <summary>
        /// Set User Information - Set's the local user information and the initial nickname the user has.
        /// This is essentially only done when you connect to the server
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sender"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool SetUserInformation(string name, NetConnection sender, NetPipeMessage msg)
        {
            var userInfo = msg.GetMessage <UserInfoMessage>();

            if (userInfo == null)
            {
                return(false);
            }

            // Default first channel as the selected channel.
            CurrentChannel = userInfo.ChatChannels.First();
            _chatChannels  = userInfo.ChatChannels;

            // update the channel list
            UpdateChannelList(_chatChannels);

            // assign current user as default
            LocalUser = userInfo.UserData;

            // set default to be current channel and load chat history
            OnChannelSet(CurrentChannel);

            _chatTerminal.Print("My nickname is: " + userInfo.UserData.Nickname + ", to change it type /nickname yournickname also /help shows you a list of commands.");


            return(true);
        }
Пример #2
0
        /// <summary>
        /// OnChannelUpdate Network Handler
        /// Called when something changes in a channel, this is only executed on the clients.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sender"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool OnChannelUpdate(string name, NetConnection sender, NetPipeMessage msg)
        {
            var channel = msg.GetMessage <ChatChannel>();

            if (channel == null)
            {
                return(true);                 // error
            }
            // check if channel already exists
            var localChannel = GetChannelByName(channel.Name);

            if (localChannel != null)
            {
                localChannel.Participants = channel.Participants;

                // only update visual list if that channel is selected.
                if (CurrentChannel == localChannel)
                {
                    OnChannelUserUpdate(localChannel);
                }
            }
            else
            {
                // add channel to list, participants came from server so it is trustworthy.
                _chatChannels.Add(channel);

                // update the channel list
                UpdateChannelList(_chatChannels);
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Say Message Handler - When server tells us someone has sent a message.
        /// </summary>
        /// <param name="name">the message name</param>
        /// <param name="sender">the sender information (server)</param>
        /// <param name="msg">the message which was sent across the network</param>
        /// <returns></returns>
        private bool SayMessageReceived(string name, NetConnection sender, NetPipeMessage msg)
        {
            // Retrieve the message
            var chatMessage = msg.GetMessage <Message>();

            // this can be null if someone is sending data which is erroneous or they're sending the wrong arguments
            if (chatMessage == null)
            {
                return(false);
            }

            // filter text - bad words, smiley conversion
            chatMessage.Text = FilterHandler(chatMessage.Text);

            // message is for other channel
            if (chatMessage.ChannelName == CurrentChannel.Name)
            {
                _chatTerminal.Print(chatMessage.User.Nickname + ": " + chatMessage.Text);
            }

            // get channel by name
            var sourceChannel = GetChannelByName(chatMessage.ChannelName);

            sourceChannel.Messages.Add(chatMessage);

            Log.Information("Recieved message:" + chatMessage.Text);
            Debug.Log("Nickname said: " + chatMessage.Text);
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Request nickname handler for client
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sender"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool NicknameChangeRequest(string name, NetConnection sender, NetPipeMessage msg)
        {
            var message = msg.GetMessage <User>();

            if (message != null)
            {
                var newNickname = CleanString(message.Nickname);

                if (newNickname.Length > 3)
                {
                    var senderUser  = GetUser(sender);
                    var oldnickname = senderUser.Nickname;

                    if (Users.SingleOrDefault(data => data.Key.Nickname == newNickname).Key != null)
                    {
                        Log.Information("Someone tried to change their username and it's already in use.");
                        return(false);
                    }
                    else
                    {
                        // update nickname
                        senderUser.Nickname = newNickname;

                        // tell client they have new nickname - only tell the exact client
                        _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                        {
                            Text = "Nickname changed to " + senderUser.Nickname
                        });

                        var channels = GetChannelByUser(senderUser);

                        // update all channels with the new nickname
                        foreach (var channel in channels)
                        {
                            _server.NetworkPipe.SendReliable("ChannelUpdate", channel);
                        }

                        Log.Information("user nickname changed from {oldname} to {nickname}", oldnickname, senderUser.Nickname);
                    }
                }
                else
                {
                    // tell client they have new nickname - only tell the exact client
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = "Sorry the new nickname is not long enough"
                    });
                    Log.Information("Someone tried to change their username and it wasn't long enough.");
                }

                return(false);
            }
            return(true);
        }
Пример #5
0
        /// <summary>
        /// On Nickname Response
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sender"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool OnServerNotification(string name, NetConnection sender, NetPipeMessage msg)
        {
            var message = msg.GetMessage <Message>();

            if (message != null)
            {
                _chatTerminal.Print("[server] " + message.Text);
            }

            return(true);
        }
Пример #6
0
        public bool RequestInviteUser(string name, NetConnection sender, NetPipeMessage msg)
        {
            var invite = msg.GetMessage <UserChannelInvite>();

            if (invite != null)
            {
                Log.Information("Processing channel invite {channel} nickname {nickname}", invite.ChannelName, invite.Nickname);

                var user    = GetuserByName(invite.Nickname);
                var channel = GetChannelByName(invite.ChannelName);

                if (channel.Participants.Contains(user))
                {
                    // tell client that this user is already in this group
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = "Sorry, you can't add a user to the same group twice."
                    });
                    return(false);
                }

                if (channel == _globalChannel)
                {
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = "Sorry, you can't invite users to the global channel."
                    });
                    return(false);
                }

                if (user != null && channel != null)
                {
                    Log.Information("Channel found {channel}, and user found too {user}", channel.Name, user.Nickname);
                    channel.Participants.Add(user);

                    // send all the members of the channel an update with the new member
                    _server.NetworkPipe.SendReliable("ChannelUpdate", channel, GetParticipantConnections(channel));

                    // tell client that this user is already in this group
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = "New user added to the group: " + user.Nickname
                    });
                }
                else
                {
                    Log.Error("Failed to find channel or user {invite}", invite);
                }
            }

            return(false);
        }
Пример #7
0
        public bool RequestNewGroup(string name, NetConnection sender, NetPipeMessage msg)
        {
            var channel = msg.GetMessage <ChatChannel>();

            if (channel != null)
            {
                Log.Information("New group requested: {name}", channel.Name);
                // make sure channel doesn't already exist
                if (GetChannelByName(channel.Name) == null)
                {
                    Channels.Add(channel);
                    channel.Participants = new List <User>
                    {
                        GetUser(sender)
                    };
                    // add this user to the channel
                    _server.NetworkPipe.SendClient(sender, "ChannelUpdate", channel);
                    Log.Information("Group creation completed: {name}", channel.Name);

                    // tell client we created the group, and tell them what to do.
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = channel.Name + " <b>group created</b>, it's now in the tabs at the top, click on it! then /invite anotherusername to invite users to your group."
                    });
                }
                else
                {
                    // tell client we couldn't make a group with that name
                    _server.NetworkPipe.SendClient(sender, "OnServerNotification", new Message
                    {
                        Text = "Sorry a group with that name already exists " + channel.Name
                    });
                    Log.Information("Group already exists error {name}", channel.Name);
                    // channel can't be made it already exists.
                    // notify("channel can't be created already exists, try another name")
                }
            }

            return(false);
        }
Пример #8
0
        /// <summary>
        /// Say Text Handler
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sender"></param>
        /// <param name="msg"></param>
        public bool SayMessageReceived(string name, NetConnection sender, NetPipeMessage msg)
        {
            // Retrieve the message
            var chatMessage = msg.GetMessage <Message>();

            // this can be null if someone is sending data which is erroneous or they're sending the wrong arguments
            if (chatMessage == null)
            {
                return(false);
            }

            // get the sender user
            var senderUser = GetUser(sender);

            // make sure this user has been registered properly and has a valid session
            if (senderUser != null)
            {
                // apply the user info to the message (only safe to do serverside)
                chatMessage.User = senderUser;
                Log.Debug("Found valid user for {sender} attaching User to message for retransmission", sender);
            }
            else
            {
                // exit, someone is probably trying to fake being another user
                Log.Error("Someone caught trying to be another user: {sender}", sender);
                return(false);
            }

            // process message and send to clients which should recieve the message
            var server = GameServiceManager.GetService <NetworkServer>();

            Log.Information("Recieved message:" + chatMessage.Text);

            // todo: add other channel support

            // if channel is not supplied then it's for the global channel
            var channel = GetChannelByName(chatMessage.ChannelName) ?? _globalChannel;

            if (channel == null)
            {
                Log.Error("Error channel not found");
            }

            // add conversation message to history
            channel.Messages.Add(chatMessage);

            var participantsCount = channel.Participants.Count;
            var userConnections   = new List <NetConnection>();

            // send message to all participants of the channel
            foreach (var user in channel.Participants)
            {
                if (Users.ContainsKey(user))
                {
                    userConnections.Add(Users[user]);
                }
            }

            // send message to all clients
            server.NetworkPipe.SendReliable("say", chatMessage, userConnections);
            return(true);
        }