#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 {
StrBuf *Chardata; // XML chardata in between tags is built up here
StrBuf *Hrefs; // list of items requested by a `calendar-multiget` REPORT
Array *filters; // If the query contains a FILTER stanza, the filter criteria are populated here
+ int filter_nest; // tag nesting level where a FILTER stanza begins
};
int i;
char indent[256];
indent[0] = 0;
- lor (i=0; i<crp->tag_nesting_level; ++i) {
+ for (i=0; i<crp->tag_nesting_level; ++i) {
strcat(indent, " ");
}
syslog(LOG_DEBUG, "%s<%s>", indent, el);
+#endif
++crp->tag_nesting_level;
+#ifdef DEBUG_XML_PARSE
indent[0] = 0;
for (i=0; i<crp->tag_nesting_level; ++i) {
strcat(indent, " ");
// RFC4791 9.7 create a filter array if this query contains a "filter" stanza
else if (!strcasecmp(el, CAL"filter")) {
crp->filters = array_new(SIZ);
+ crp->filter_nest = crp->tag_nesting_level;
}
// Handle the filters defined in RFC4791 9.7.1 through 9.7.5
)
&& (crp->filters) // Make sure we actually allocated an array
) {
+
char newfilter[SIZ];
int a = 0;
- int len = snprintf(newfilter, SIZ, &el[CALLEN]); // strip off the namespace for our purposes
+ int len = snprintf(newfilter, SIZ, "%d|", crp->tag_nesting_level - crp->filter_nest - 1);
+ len += snprintf(&newfilter[len], SIZ-len, "%s", &el[CALLEN]); // filter name without the namespace
while (attr[a]) {
len += snprintf(&newfilter[len], SIZ-len, "|%s", attr[a++]); // now save the attributes
}
}
-// 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;
- }
-
- // BEGIN experimental block -- see what happens when we cast to the wrong type
-
- //icalcomponent *foo_comp = (icalcomponent *) cal;
- //icalproperty *foo_prop = (icalproperty *) cal;
- //icalparameter *foo_param = (icalparameter *) cal;
-
- // END experimental block
+// 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 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) ) {
+
+ // 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 level = atoi(t[0]);
+ syslog(LOG_DEBUG, "caldav_apply_filters() filter=%d, level=%d, <%s>", f, level, array_get_element_at(filters, f) );
- // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
+ // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
- if (!strcasecmp(t[0], "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");
+ if (level < previous_level) {
+ syslog(LOG_DEBUG, "caldav: walking back down");
+ return(qual);
}
- else {
- syslog(LOG_DEBUG, "\033[31m no this is not a component \033[0m");
+
+ 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[0], "prop-filter")) { // RFC4791 9.7.2 - filter by property
- syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
- }
+ else if (!strcasecmp(t[1], "comp-filter")) { // RFC4791 9.7.1 - filter by component
+ syslog(LOG_DEBUG, "component filter at level %d", level);
- else if (!strcasecmp(t[0], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
- syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
- }
+ // Root element is NOT a component, but the root filter is "comp-filter" -- reject!
+ if ( (!icalcomponent_isa_component(cal)) && (level == 0) ) {
+ syslog(LOG_DEBUG, "caldav: root element is not a component, rejecting");
+ return(0);
+ }
- else if (!strcasecmp(t[0], "is-not-defined")) { // RFC4791 9.7.4
- syslog(LOG_DEBUG, "is-not-defined 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))
+ //&& (level == 0)
+ && (!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...",
+ 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 if (!strcasecmp(t[0], "text-match")) { // RFC4791 9.7.5
- syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
- }
+ }
+ 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);
+ }
+ }
- return(1);
-}
+ }
+ else if (!strcasecmp(t[1], "prop-filter")) { // RFC4791 9.7.2 - filter by property
+ syslog(LOG_DEBUG, "property 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], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
+ syslog(LOG_DEBUG, "parameter 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], "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");
+ }
- // 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", start_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) {