private void SendPrivateMessage(Client client, string[] parameters) { if (parameters.Length < 2) { client.DisplayString("Incorrect syntax. Use /pm [username] [message]"); return; } var username = parameters[0]; var message = new StringBuilder(); for (var i = 1; i < parameters.Length; i++) { if (i > 1) message.Append(" "); message.Append(parameters[i]); } if (username == client.User.Username) { client.DisplayString("You can't send private messages to yourself."); return; } if (!Server.GetServer().IsUserOnline(username)) { client.DisplayString($"The user '{username}' is not online. Can't deliver message."); return; } var chatMessage = new ChatMessage { FromUser = client.User.Username, ToUser = username, Message = message.ToString(), Time = DateTime.Now }; client.DisplayString($"To {chatMessage.ToUser}: {chatMessage.Message}"); Server.GetServer().GetUser(chatMessage.ToUser).DisplayString($"{chatMessage.FromUser} whispers: {chatMessage.Message}"); Database.GetDatabase().ChatMessages.Add(chatMessage); BetterConsole.WriteLog($"Private message sent: {client.User.Username} -> {username}"); }
private void ClientThread() { while (_clientSocket.Connected) { var message = string.Empty; try { message = Stream.Reader.ReadString(); } catch (Exception) { break; } if (message.StartsWith("/")) { ProcessCommand(message); continue; } if (!Authenticated) { DisplayString("You are not authenticated to use the chat. Please login first using '/login [username] [password]'"); continue; } var chatMessage = new ChatMessage { FromUser = User.Username, ToUser = "******", Message = message, Time = DateTime.Now }; Server.GetServer().BroadcastChatMessage(chatMessage); Database.GetDatabase().ChatMessages.Add(chatMessage); } Server.GetServer().RemoveClient(this); if (Authenticated) Server.GetServer().BroadcastMessage($"{User.Username} has left the chat."); }
public void BroadcastChatMessage(ChatMessage chatMessage) { BroadcastMessage($"{chatMessage.FromUser} says: {chatMessage.Message}"); }