protected virtual void OnJoined(ChatUserPeerExtension newUser) { var data = new List <string>() { Name, newUser.Username }; var msg = Mst.Create.Message((short)MstMessageCodes.UserJoinedChannel, data.ToBytes()); foreach (var user in _users.Values) { if (user != newUser) { user.Peer.SendMessage(msg, DeliveryMethod.Reliable); } } }
protected virtual void OnLeft(ChatUserPeerExtension removedUser) { var data = new List <string>() { Name, removedUser.UserId, removedUser.Username }; var msg = Mst.Create.Message((short)MstMessageCodes.UserLeftChannel, data.ToBytes()); foreach (var user in channelUsers.Values) { if (user.UserId != removedUser.UserId) { 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 channelUsers.Remove(user.UserId); 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(!channelUsers.ContainsKey(user.UserId)); }
protected virtual void OnPickUsernameRequestHandler(IIncomingMessage message) { string responseMsg; 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, IIncomingMessage 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)MstMessageCodes.ChatMessage, message); rawMessage.Respond(ResponseStatus.Success); return(true); } return(false); }
/// <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, IIncomingMessage rawMessage) { try { // Set a true sender message.Sender = sender.Username; 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) { throw new MstMessageHandlerException("No channel is set to be your local channel", ResponseStatus.Failed); } sender.DefaultChannel.BroadcastMessage(message); rawMessage.Respond(ResponseStatus.Success); return(true); } // Find the channel if (!ChatChannels.TryGetValue(message.Receiver, out ChatChannel channel) || !sender.CurrentChannels.Contains(channel)) { throw new MstMessageHandlerException($"You're not in the '{message.Receiver}' channel", ResponseStatus.Failed); } channel.BroadcastMessage(message); rawMessage.Respond(ResponseStatus.Success); return(true); case ChatMessageType.PrivateMessage: if (!ChatUsers.TryGetValue(message.Receiver, out ChatUserPeerExtension receiver)) { throw new MstMessageHandlerException($"User '{message.Receiver}' is not online", ResponseStatus.Failed); } receiver.Peer.SendMessage((short)MstMessageCodes.ChatMessage, message); rawMessage.Respond(ResponseStatus.Success); return(true); } return(false); } // If we got system exception catch (MstMessageHandlerException e) { logger.Error(e.Message); rawMessage.Respond(e.Message, e.Status); return(false); } // If we got another exception catch (Exception e) { logger.Error(e.Message); rawMessage.Respond(e.Message, ResponseStatus.Error); return(false); } }