#include <sourcemod>
#include <sdktools>

// Define the maximum allowed length for data
#define MAX_ALLOWED_LENGTH 32768

// Define buffer size (limited to the size of incoming message)
#define BUFFER_SIZE 512

// Function to handle suspicious clients and kick them
void HandleSuspiciousClient(int client) {
    // Log the suspicious action
    char name[64];
    GetClientName(client, name, sizeof(name));
    LogError("Client %s (ID %d) attempted to send data larger than allowed size.", name, client);

    // Kick the client
    KickClient(client, "Attempted to exploit the server.");

    // Inform the server
    PrintToServer("Client %s (ID %d) was kicked for attempting to exploit the server.", name, client);
}

// Function to check if the data size is suspicious (based on buffer size)
bool IsDataSizeSuspicious(const char[] data) {
    return (strlen(data) > BUFFER_SIZE);
}

// Event handler for client chat messages
public void OnPluginStart() {
    // Hook the event for client chat messages
    HookEvent("player_say", Event_PlayerSay, EventHookMode_Post);
}

// Event handler function
public Action Event_PlayerSay(Handle event, const char[] name, bool dontBroadcast) {
    int client = GetClientOfUserId(GetEventInt(event, "userid"));
    char data[BUFFER_SIZE]; // Buffer size for incoming message

    // Get data from the event
    GetEventString(event, "text", data, sizeof(data));

    // Check the client data (limited to the size of the buffer)
    if (IsDataSizeSuspicious(data)) {
        HandleSuspiciousClient(client);
    }

    // Continue the event execution
    return Plugin_Continue;
}

// Cleanup on plugin end
public void OnPluginEnd() {
    // Unhook the event
    UnhookEvent("player_say", Event_PlayerSay);
}