/// <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); }
protected virtual bool AddChatUser(ChatUserExtension user) { // Add the new user ChatUsers[user.Username.ToLower()] = user; user.Peer.Disconnected += OnClientDisconnected; return(true); }
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; }
protected virtual void OnLeft(ChatUserExtension removedUser) { var data = new List <string> { Name, removedUser.Username }; var msg = Msf.Create.Message((short)MsfOpCodes.UserLeftChannel, data.ToBytes()); foreach (var user in _users.Values) { if (user != removedUser) { user.Peer.SendMessage(msg, DeliveryMethod.Reliable); } } }
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); }
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); }
/// <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); }
protected virtual bool IsUserAllowed(ChatUserExtension user) { // Can't join if already here return(!_users.ContainsKey(user.Username)); }
protected virtual void RemoveChatUser(ChatUserExtension user) { ChatUsers.Remove(user.Username.ToLower()); user.Peer.Disconnected -= OnClientDisconnected; }