#include "webcit.h"
-// Shorthand for the XML namespace of CalDAV
-#define CAL "urn:ietf:params:xml:ns:caldav:"
-#define CALLEN sizeof(CAL)-1
+#define CAL "urn:ietf:params:xml:ns:caldav:" // Shorthand for the XML namespace of CalDAV
+#define CALLEN sizeof(CAL)-1 // And the length of that string
// A CalDAV REPORT can only be one type. This is stored in the report_type member.
enum cr_type {
|| (!strcasecmp(el, CAL"param-filter"))
|| (!strcasecmp(el, CAL"is-not-defined"))
|| (!strcasecmp(el, CAL"text-match"))
+ || (!strcasecmp(el, CAL"time-range"))
)
&& (crp->filters) // Make sure we actually allocated an array
) {
}
-// Called by caldav_apply_filters() to apply ONE filter.
-// Returns 0 if the calendar item does not match the filter.
-// Returns 1 if the calendar item DOES match the filter.
-int caldav_apply_one_filter(void *cal, char *filter) {
- syslog(LOG_DEBUG, "applying filter: %s", filter);
-
- char this_filter[SIZ]; // we have to copy the filter string because we will destructively tokenize it
- safestrncpy(this_filter, filter, sizeof(this_filter));
-
- char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
- char *f = this_filter;
- int num_tokens = 0;
- while ( (t[num_tokens]=strtok_r(f, "|", &f)) && (num_tokens<10) ) {
- ++num_tokens;
- }
+// 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 apply_at_level) {
+
+ int f = 0; // filter number iterator
+ int qual = 1; // 0 for disqualify, 1 for qualify
+ int previous_level = -1;
+ int disregard_further_comp_filters = 0;
+
+ while ( (f<array_len(filters)) && (qual) ) {
+
+ // Tokenize the filter (a future performance hack would be to pre-tokenize instead of storing delimited strings)
+ char this_filter[SIZ];
+ safestrncpy(this_filter, array_get_element_at(filters, f), sizeof(this_filter));
+ char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
+ char *ft = this_filter;
+ int num_tokens = 0;
+ while ( (t[num_tokens]=strtok_r(ft, "|", &ft)) && (num_tokens<10) ) {
+ ++num_tokens;
+ }
+ int this_rule_level = atoi(t[0]);
+ syslog(LOG_DEBUG, "caldav_apply_filters() filter=%d, level=%d, <%s>", f, this_rule_level, array_get_element_at(filters, f) );
- // BEGIN experimental block -- see what happens when we cast to the wrong type
+ // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
- //icalcomponent *foo_comp = (icalcomponent *) cal;
- //icalproperty *foo_prop = (icalproperty *) cal;
- //icalparameter *foo_param = (icalparameter *) cal;
+ if (apply_at_level < previous_level) {
+ syslog(LOG_DEBUG, "caldav: walking back down");
+ return(qual);
+ }
- // END experimental block
+ else if (this_rule_level != apply_at_level) {
+ syslog(LOG_DEBUG, "caldav: apply_at_level=%d, this_rule_level=%d, skipping this rule", apply_at_level, this_rule_level);
+ }
- // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
+ else if ( (!strcasecmp(t[1], "comp-filter"))
+ && (!disregard_further_comp_filters)
+ ) { // RFC4791 9.7.1 - filter by component
+ syslog(LOG_DEBUG, "component filter at level %d", this_rule_level);
- if (!strcasecmp(t[1], "comp-filter")) { // RFC4791 9.7.1 - filter by component
- syslog(LOG_DEBUG, "component filter FIXME not implemented yet");
- if (icalcomponent_isa_component(cal)) {
- syslog(LOG_DEBUG, "\033[32m yes this is a component \033[0m");
- }
- else {
- syslog(LOG_DEBUG, "\033[31m no this is not a component \033[0m");
- }
- }
+ // Root element is NOT a component, but the root filter is "comp-filter" -- reject!
+ if ( (!icalcomponent_isa_component(cal)) && (this_rule_level == 0) ) {
+ syslog(LOG_DEBUG, "caldav: root element is not a component, rejecting");
+ return(0);
+ }
- else if (!strcasecmp(t[1], "prop-filter")) { // RFC4791 9.7.2 - filter by property
- syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
- }
+ // Current element is a component and the filter is "comp-filter" -- see if it matches the requested type
+ if ( (icalcomponent_isa_component(cal))
+ && (!strcasecmp(t[2], "name"))
+ ) {
+ if (icalcomponent_isa(cal) == icalcomponent_string_to_kind(t[3]) ) {
+ syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, recursing...",
+ apply_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, apply_at_level+1)) {
+ syslog(LOG_DEBUG, "Subcomponent %d might match", number_of_subcomponents);
+ ++number_of_matches;
+ }
+ }
+ if (number_of_matches > 0) { // something matched
+ qual = 1;
+ disregard_further_comp_filters = 1;
+
+ }
+ else if (number_of_subcomponents > 0) { // nothing matched
+ return(0); // but only fail if there *were* subcomponents.
+ }
- else if (!strcasecmp(t[1], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
- syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
- }
+ }
+ else {
+ syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, rejecting",
+ apply_at_level,
+ icalcomponent_kind_to_string(icalcomponent_isa(cal)),
+ t[3]
+ );
+ return(0);
+ }
+ }
- else if (!strcasecmp(t[1], "is-not-defined")) { // RFC4791 9.7.4
- syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
- }
+ }
- else if (!strcasecmp(t[1], "text-match")) { // RFC4791 9.7.5
- syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
- }
+ else if (!strcasecmp(t[1], "prop-filter")) { // RFC4791 9.7.2 - filter by property
+ syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
+ }
- return(1);
-}
+ else if (!strcasecmp(t[1], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
+ syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
+ }
+ else if (!strcasecmp(t[1], "is-not-defined")) { // RFC4791 9.7.4
+ syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
+ }
-// 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 index) {
+ else if (!strcasecmp(t[1], "text-match")) { // RFC4791 9.7.5
+ syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
+ }
- // Apply *this* filter.
- if (caldav_apply_one_filter(cal, array_get_element_at(filters, index)) == 0) {
- return(0);
- }
+ else if (!strcasecmp(t[1], "time-range")) {
+ syslog(LOG_DEBUG, "time range filter \033[33mFIXME\033[0m not implemented yet");
+ }
- // If we get to this point, the current filter has passed, and we move on to the next one.
- if (index < array_len(filters)-1) {
- return(caldav_apply_filters(cal, filters, index+1));
+ ++f;
}
- // If we got this far, every filter passed, and the calendar item qualifies for output.
- return(1);
+ syslog(LOG_DEBUG, "caldav: we reached the end of level %d , returning %d", apply_at_level, qual);
+ return(qual);
}
int qualify = 1;
// 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, 0);
- syslog(LOG_DEBUG, "Message %ld does%s qualify", m, (qualify ? "" : " NOT"));
+ syslog(LOG_DEBUG, "Message %ld %s\033[0m qualify", m, (qualify ? "\033[32mDOES" : "\033[31mDOES NOT"));
+ syslog(LOG_DEBUG, "");
// Did this calendar item match the query? If so, output it.
if (qualify) {