]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/server/caldav_reports.c
XML is like violence: if it doesn't work, you're not using enough.
[citadel.git] / webcit-ng / server / caldav_reports.c
index 05d3b9ef65e9b37c5b92a26f280e9d2b9f063fbf..76a0ee43d1438a9c65999552254827f3b752d6b9 100644 (file)
@@ -274,13 +274,13 @@ void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, S
 
 // 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) ) {
-               syslog(LOG_DEBUG, "caldav_apply_filters(%d) %s", f, array_get_element_at(filters, f) );
 
                // Tokenize the filter (a future performance hack would be to pre-tokenize instead of storing delimited strings)
                char this_filter[SIZ];
@@ -292,14 +292,72 @@ int caldav_apply_filters(void *cal, Array *filters) {
                        ++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
 
-               if (!strcasecmp(t[1], "comp-filter")) {                         // RFC4791 9.7.1 - filter by component
-                       syslog(LOG_DEBUG, "component filter at level %d FIXME not implemented yet", level);
-                       if (icalcomponent_isa_component(cal)) {
-                               // yes this is a 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!
+                       if ( (!icalcomponent_isa_component(cal)) && (level == 0) ) {
+                               syslog(LOG_DEBUG, "caldav: root element is not a component, rejecting");
+                               return(0);
                        }
+
+                       // 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 {
+                                       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);
+                               }
+                       }
+
                }
 
                else if (!strcasecmp(t[1], "prop-filter")) {                    // RFC4791 9.7.2 - filter by property
@@ -321,6 +379,7 @@ int caldav_apply_filters(void *cal, Array *filters) {
                ++f;
        }
 
+       syslog(LOG_DEBUG, "caldav: we reached the end of level %d , returning %d", start_at_level, qual);
        return(qual);
 }
 
@@ -386,8 +445,10 @@ void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
                                int qualify = 1;
 
                                // If there was a filter stanza, run this calendar item through the filters.
-                               qualify = caldav_apply_filters(cal, crp.filters);
-                               syslog(LOG_DEBUG, "Message %ld does%s qualify", m, (qualify ? "" : " NOT"));
+                               syslog(LOG_DEBUG, "Evaluating message \033[33m%ld\033[0m...", m);
+                               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, "");
 
                                // Did this calendar item match the query?  If so, output it.
                                if (qualify) {