protected virtual void OnLeft(ChatUserPeerExtension removedUser) { var data = new List <string>() { Name, removedUser.Username }; var msg = Msf.Create.Message((short)MsfMessageCodes.UserLeftChannel, data.ToBytes()); foreach (var user in _users.Values) { if (user != removedUser) { user.Peer.SendMessage(msg, DeliveryMethod.Reliable); } } }
public void RemoveUser(ChatUserPeerExtension user) { // Remove disconnect listener user.Peer.OnPeerDisconnectedEvent -= 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); }
/// <summary> /// Returns true, if user successfully joined a channel /// </summary> /// <param name="user"></param> /// <returns></returns> public bool AddUser(ChatUserPeerExtension user) { if (!IsUserAllowed(user)) { return(false); } // Add disconnect listener user.Peer.OnPeerDisconnectedEvent += OnUserDisconnect; // Add user _users.Add(user.Username, user); // Add channel to users collection user.CurrentChannels.Add(this); OnJoined(user); return(true); }
/// <summary> /// Remove user from chat /// </summary> /// <param name="user"></param> protected virtual void RemoveChatUser(ChatUserPeerExtension user) { string username = user.Username.ToLower(); // Remove from chat users list ChatUsers.Remove(username); var channels = user.CurrentChannels.ToList(); // Remove from channels foreach (var chatChannel in channels) { chatChannel.RemoveUser(user); } // Stop listening this removed user disconnection user.Peer.OnPeerDisconnectedEvent -= OnClientDisconnected; logger.Debug($"User {username} has been successfully removed from chat"); }
/// <summary> /// Add new user to chat /// </summary> /// <param name="user"></param> /// <returns></returns> protected virtual bool AddChatUser(ChatUserPeerExtension user) { string username = user.Username.ToLower(); if (ChatUsers.ContainsKey(username)) { logger.Error($"Trying to add user {username} to chat, but one is already connected"); return(false); } else { // Add the new user ChatUsers[user.Username.ToLower()] = user; // Start listening user disconnection user.Peer.OnPeerDisconnectedEvent += OnClientDisconnected; logger.Debug($"User {username} has been successfully added to chat"); return(true); } }
protected virtual bool IsUserAllowed(ChatUserPeerExtension user) { // Can't join if already here return(!_users.ContainsKey(user.Username)); }
protected virtual void OnPickUsernameRequestHandler(IIncommingMessage message) { string responseMsg = string.Empty; if (!allowUsernamePicking) { responseMsg = "Username picking is disabled"; logger.Error(responseMsg); message.Respond(responseMsg, ResponseStatus.Failed); return; } var username = message.AsString(); if (username.Contains(" ")) { responseMsg = "Username cannot contain whitespaces"; logger.Error(responseMsg); message.Respond(responseMsg, ResponseStatus.Failed); return; } var chatUser = message.Peer.GetExtension <ChatUserPeerExtension>(); if (chatUser != null) { responseMsg = $"You're already identified as: {chatUser.Username}"; logger.Error(responseMsg); message.Respond(responseMsg); return; } if (ChatUsers.ContainsKey(username.ToLower())) { responseMsg = "There's already a user who has the same username"; logger.Error(responseMsg); message.Respond(responseMsg, ResponseStatus.Failed); return; } chatUser = new ChatUserPeerExtension(message.Peer, username); if (!AddChatUser(chatUser)) { responseMsg = "Failed to add user to chat"; logger.Error(responseMsg); message.Respond(responseMsg, ResponseStatus.Failed); return; } // Add the extension message.Peer.AddExtension(chatUser); message.Respond(ResponseStatus.Success); }
/// <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); }