// Recursive function to apply CalDAV FILTERS to a calendar item.
// Returns zero if the calendar item was disqualified by a filter, nonzero if the calendar item still qualifies.
-int caldav_apply_filters(void *cal, Array *filters) {
+int caldav_apply_filters(void *cal, Array *filters, int start_at_level) {
int f = 0; // filter number iterator
int qual = 1; // 0 for disqualify, 1 for qualify
+ int previous_level = -1;
while ( (f<array_len(filters)) && (qual) ) {
// Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
- if (!strcasecmp(t[1], "comp-filter")) { // RFC4791 9.7.1 - filter by component
+ if (level < previous_level) {
+ syslog(LOG_DEBUG, "caldav: walking back down");
+ return(qual);
+ }
+
+ else if (level < start_at_level) {
+ syslog(LOG_DEBUG, "caldav: start_at_level=%d, level=%d, skipping this rule", start_at_level, level);
+ }
+
+ else if (!strcasecmp(t[1], "comp-filter")) { // RFC4791 9.7.1 - filter by component
syslog(LOG_DEBUG, "component filter at level %d", level);
// Root element is NOT a component, but the root filter is "comp-filter" -- reject!
return(0);
}
- // Root element IS a component and the root filter is "comp-filter" -- see if it matches the requested type
+ // Current element is a component and the filter is "comp-filter" -- see if it matches the requested type
if ( (icalcomponent_isa_component(cal))
- && (level == 0)
+ //&& (level == 0)
&& (!strcasecmp(t[2], "name"))
) {
- if (icalcomponent_isa(cal) != icalcomponent_string_to_kind(t[3]) ) {
- syslog(LOG_DEBUG, "caldav: root component is <%s>, looking for <%s>, rejecting",
+ if (icalcomponent_isa(cal) == icalcomponent_string_to_kind(t[3]) ) {
+ syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, recursing...",
+ start_at_level,
icalcomponent_kind_to_string(icalcomponent_isa(cal)), t[3]
);
+
+ // We have a match. Drill down into the subcomponents.
+
+ icalcomponent *c = NULL;
+ int number_of_subcomponents = 0;
+ int number_of_matches = 0;
+ for ( c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
+ (c != 0);
+ c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)
+ ) {
+ ++number_of_subcomponents;
+ if (caldav_apply_filters(c, filters, level+1)) {
+ syslog(LOG_DEBUG, "Subcomponent %d might match", number_of_subcomponents);
+ ++number_of_matches;
+ }
+ }
+ if (number_of_matches > 0) { // something matched
+ qual = 1;
+ }
+ else if (number_of_subcomponents > 0) { // nothing matched
+ return(0); // but only fail if there *were* subcomponents.
+ }
+
+ }
+ else {
+ syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, rejecting",
+ start_at_level,
+ icalcomponent_kind_to_string(icalcomponent_isa(cal)),
+ t[3]
+ );
return(0);
}
}
++f;
}
+ syslog(LOG_DEBUG, "caldav: we reached the end of level %d , returning %d", start_at_level, qual);
return(qual);
}
// If there was a filter stanza, run this calendar item through the filters.
syslog(LOG_DEBUG, "Evaluating message \033[33m%ld\033[0m...", m);
- qualify = caldav_apply_filters(cal, crp.filters);
+ qualify = caldav_apply_filters(cal, crp.filters, 0);
syslog(LOG_DEBUG, "Message %ld %s\033[0m qualify", m, (qualify ? "\033[32mDOES" : "\033[31mDOES NOT"));
syslog(LOG_DEBUG, "");