+// 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;
+ }
+
+ return(1);
+}
+
+
+// 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) {
+
+ // Apply *this* filter.
+ if (caldav_apply_one_filter(cal, array_get_element_at(filters, index)) == 0) {
+ return(0);
+ }
+
+ // 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));
+ }
+
+ // If we got this far, every filter passed, and the calendar item qualifies for output.
+ return(1);
+}
+
+