Sanitize Chat

This commit is contained in:
TheBrokenRail 2021-02-21 22:16:05 -05:00
parent 615a3a28a0
commit 7b6fa15893

View File

@ -1,4 +1,5 @@
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <pthread.h>
@ -10,6 +11,11 @@
#include "chat.h"
// Message Limitations
#define MAX_MESSAGE_LENGTH 512
#define MINIMUM_MESSAGE_CHARACTER 32
#define MAXIMUM_MESSAGE_CHARACTER 126
// Send API Command
static void send_api_command(unsigned char *minecraft, char *str) {
struct ConnectedClient client;
@ -30,11 +36,29 @@ static void send_api_chat_command(unsigned char *minecraft, char *str) {
free(command);
}
// Sanitize Message
static void sanitize_message(char **message) {
// Store Message Length
int length = strlen(*message);
// Truncate Message
if (length > MAX_MESSAGE_LENGTH) {
(*message)[MAX_MESSAGE_LENGTH] = '\0';
length = MAX_MESSAGE_LENGTH;
}
// Loop Through Message
for (int i = 0; i < length; i++) {
if ((*message)[i] < MINIMUM_MESSAGE_CHARACTER || (*message)[i] > MAXIMUM_MESSAGE_CHARACTER) {
// Replace Illegal Character
(*message)[i] = '?';
}
}
}
// Send Message To Players
static void send_message(unsigned char *server_side_network_handler, char *username, char *message) {
char *full_message = NULL;
asprintf(&full_message, "<%s> %s", username, message);
ALLOC_CHECK(full_message);
sanitize_message(&full_message);
(*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, std::string(full_message));
free(full_message);
}