v0.4.11 - Fixed nasty DM bug

This commit is contained in:
Your Name
2025-10-06 10:06:24 -04:00
parent d5350d7c30
commit f6d13d4318
11 changed files with 1376 additions and 753 deletions

View File

@@ -269,26 +269,39 @@ int event_matches_filter(cJSON* event, subscription_filter_t* filter) {
if (!event || !filter) {
return 0;
}
// Check kinds filter
if (filter->kinds && cJSON_IsArray(filter->kinds)) {
cJSON* event_kind = cJSON_GetObjectItem(event, "kind");
if (!event_kind || !cJSON_IsNumber(event_kind)) {
log_info("DEBUG FILTER: kinds filter present but event has no valid kind");
return 0;
}
int event_kind_val = (int)cJSON_GetNumberValue(event_kind);
int kind_match = 0;
char debug_kinds_msg[256];
snprintf(debug_kinds_msg, sizeof(debug_kinds_msg), "DEBUG FILTER: Checking kinds - event kind: %d", event_kind_val);
log_info(debug_kinds_msg);
cJSON* kind_item = NULL;
cJSON_ArrayForEach(kind_item, filter->kinds) {
if (cJSON_IsNumber(kind_item) && (int)cJSON_GetNumberValue(kind_item) == event_kind_val) {
kind_match = 1;
break;
if (cJSON_IsNumber(kind_item)) {
int filter_kind = (int)cJSON_GetNumberValue(kind_item);
char debug_kind_check_msg[256];
snprintf(debug_kind_check_msg, sizeof(debug_kind_check_msg), "DEBUG FILTER: Comparing event kind %d with filter kind %d", event_kind_val, filter_kind);
log_info(debug_kind_check_msg);
if (filter_kind == event_kind_val) {
kind_match = 1;
log_info("DEBUG FILTER: Kind match found!");
break;
}
}
}
if (!kind_match) {
log_info("DEBUG FILTER: No kind match - filter fails");
return 0;
}
}
@@ -377,62 +390,87 @@ int event_matches_filter(cJSON* event, subscription_filter_t* filter) {
if (filter->tag_filters && cJSON_IsObject(filter->tag_filters)) {
cJSON* event_tags = cJSON_GetObjectItem(event, "tags");
if (!event_tags || !cJSON_IsArray(event_tags)) {
log_info("DEBUG FILTER: tag filters present but event has no valid tags array");
return 0; // Event has no tags but filter requires tags
}
char debug_tags_msg[256];
snprintf(debug_tags_msg, sizeof(debug_tags_msg), "DEBUG FILTER: Checking tag filters - event has %d tags", cJSON_GetArraySize(event_tags));
log_info(debug_tags_msg);
// Check each tag filter
cJSON* tag_filter = NULL;
cJSON_ArrayForEach(tag_filter, filter->tag_filters) {
if (!tag_filter->string || strlen(tag_filter->string) < 2 || tag_filter->string[0] != '#') {
continue; // Invalid tag filter
}
const char* tag_name = tag_filter->string + 1; // Skip the '#'
char debug_tag_filter_msg[256];
snprintf(debug_tag_filter_msg, sizeof(debug_tag_filter_msg), "DEBUG FILTER: Checking tag filter #%s", tag_name);
log_info(debug_tag_filter_msg);
if (!cJSON_IsArray(tag_filter)) {
continue; // Tag filter must be an array
}
int tag_match = 0;
// Search through event tags for matching tag name and value
cJSON* event_tag = NULL;
cJSON_ArrayForEach(event_tag, event_tags) {
if (!cJSON_IsArray(event_tag) || cJSON_GetArraySize(event_tag) < 2) {
continue; // Invalid tag format
}
cJSON* event_tag_name = cJSON_GetArrayItem(event_tag, 0);
cJSON* event_tag_value = cJSON_GetArrayItem(event_tag, 1);
if (!cJSON_IsString(event_tag_name) || !cJSON_IsString(event_tag_value)) {
continue;
}
const char* event_tag_name_str = cJSON_GetStringValue(event_tag_name);
const char* event_tag_value_str = cJSON_GetStringValue(event_tag_value);
char debug_event_tag_msg[256];
snprintf(debug_event_tag_msg, sizeof(debug_event_tag_msg), "DEBUG FILTER: Event tag: %s = %s", event_tag_name_str, event_tag_value_str);
log_info(debug_event_tag_msg);
// Check if tag name matches
if (strcmp(cJSON_GetStringValue(event_tag_name), tag_name) == 0) {
const char* event_tag_value_str = cJSON_GetStringValue(event_tag_value);
if (strcmp(event_tag_name_str, tag_name) == 0) {
char debug_tag_name_match_msg[256];
snprintf(debug_tag_name_match_msg, sizeof(debug_tag_name_match_msg), "DEBUG FILTER: Tag name '%s' matches filter", tag_name);
log_info(debug_tag_name_match_msg);
// Check if any of the filter values match this tag value
cJSON* filter_value = NULL;
cJSON_ArrayForEach(filter_value, tag_filter) {
if (cJSON_IsString(filter_value)) {
const char* filter_value_str = cJSON_GetStringValue(filter_value);
char debug_filter_value_msg[256];
snprintf(debug_filter_value_msg, sizeof(debug_filter_value_msg), "DEBUG FILTER: Comparing event tag value '%s' with filter value '%s'", event_tag_value_str, filter_value_str);
log_info(debug_filter_value_msg);
// Support prefix matching for tag values
if (strncmp(event_tag_value_str, filter_value_str, strlen(filter_value_str)) == 0) {
tag_match = 1;
log_info("DEBUG FILTER: Tag value match found!");
break;
}
}
}
if (tag_match) {
break;
}
}
}
if (!tag_match) {
char debug_tag_fail_msg[256];
snprintf(debug_tag_fail_msg, sizeof(debug_tag_fail_msg), "DEBUG FILTER: Tag filter #%s failed - no match found", tag_name);
log_info(debug_tag_fail_msg);
return 0; // This tag filter didn't match, so the event doesn't match
}
}
@@ -444,17 +482,40 @@ int event_matches_filter(cJSON* event, subscription_filter_t* filter) {
// Check if an event matches any filter in a subscription (filters are OR'd together)
int event_matches_subscription(cJSON* event, subscription_t* subscription) {
if (!event || !subscription || !subscription->filters) {
char debug_msg[256];
snprintf(debug_msg, sizeof(debug_msg), "DEBUG MATCH: Subscription '%s' - invalid parameters (event: %p, subscription: %p, filters: %p)",
subscription ? subscription->id : "NULL", event, subscription, subscription ? subscription->filters : NULL);
log_info(debug_msg);
return 0;
}
char debug_start_msg[256];
snprintf(debug_start_msg, sizeof(debug_start_msg), "DEBUG MATCH: Checking subscription '%s' filters", subscription->id);
log_info(debug_start_msg);
subscription_filter_t* filter = subscription->filters;
int filter_index = 0;
while (filter) {
if (event_matches_filter(event, filter)) {
int filter_matches = event_matches_filter(event, filter);
char debug_filter_msg[256];
snprintf(debug_filter_msg, sizeof(debug_filter_msg), "DEBUG MATCH: Subscription '%s' filter %d - %s",
subscription->id, filter_index, filter_matches ? "MATCHES" : "no match");
log_info(debug_filter_msg);
if (filter_matches) {
char debug_match_msg[256];
snprintf(debug_match_msg, sizeof(debug_match_msg), "DEBUG MATCH: Subscription '%s' - MATCH FOUND", subscription->id);
log_info(debug_match_msg);
return 1; // Match found (OR logic)
}
filter = filter->next;
filter_index++;
}
char debug_no_match_msg[256];
snprintf(debug_no_match_msg, sizeof(debug_no_match_msg), "DEBUG MATCH: Subscription '%s' - NO MATCHES", subscription->id);
log_info(debug_no_match_msg);
return 0; // No filters matched
}
@@ -483,11 +544,28 @@ int broadcast_event_to_subscriptions(cJSON* event) {
}
int broadcasts = 0;
pthread_mutex_lock(&g_subscription_manager.subscriptions_lock);
// Debug: Log total active subscriptions
int total_active = 0;
subscription_t* count_sub = g_subscription_manager.active_subscriptions;
while (count_sub) {
total_active++;
count_sub = count_sub->next;
}
char debug_total_msg[128];
snprintf(debug_total_msg, sizeof(debug_total_msg), "DEBUG BROADCAST: Broadcasting event to %d total active subscriptions", total_active);
log_info(debug_total_msg);
subscription_t* sub = g_subscription_manager.active_subscriptions;
while (sub) {
// Debug: Log each subscription being checked
char debug_sub_msg[256];
snprintf(debug_sub_msg, sizeof(debug_sub_msg), "DEBUG BROADCAST: Checking subscription '%s' (active: %d)", sub->id, sub->active);
log_info(debug_sub_msg);
if (sub->active && event_matches_subscription(event, sub)) {
// Create EVENT message for this subscription
cJSON* event_msg = cJSON_CreateArray();