]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/server/caldav_reports.c
added more trace messages
[citadel.git] / webcit-ng / server / caldav_reports.c
index d93088076b8ba8a4296afb4da36ba68796213fbd..6f6fde733b50bd00e3126a08d69ef1786ffae1ef 100644 (file)
@@ -81,6 +81,7 @@ void caldav_xml_start(void *data, const char *el, const char **attr) {
                                || (!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
        ) {
@@ -274,10 +275,12 @@ 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 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) ) {
 
@@ -290,37 +293,102 @@ int caldav_apply_filters(void *cal, Array *filters) {
                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) );
+               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) );
 
                // 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 (apply_at_level < previous_level) {
+                       syslog(LOG_DEBUG, "caldav: walking back down");
+                       return(qual);
+               }
+
+               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);
+               }
+
+               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);
+
+                       // 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);
                        }
+
+                       // 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 {
+                                       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], "prop-filter")) {                    // RFC4791 9.7.2 - filter by property
-                       syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
+                       syslog(LOG_DEBUG, "property filter at level %d FIXME not implemented yet", this_rule_level);
                }
 
                else if (!strcasecmp(t[1], "param-filter")) {                   // RFC4791 9.7.3 - filter by parameter
-                       syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
+                       syslog(LOG_DEBUG, "parameter filter at level %d FIXME not implemented yet", this_rule_level);
                }
 
                else if (!strcasecmp(t[1], "is-not-defined")) {                 // RFC4791 9.7.4
-                       syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
+                       syslog(LOG_DEBUG, "is-not-defined filter at level %d FIXME not implemented yet", this_rule_level);
                }
 
                else if (!strcasecmp(t[1], "text-match")) {                     // RFC4791 9.7.5
-                       syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
+                       syslog(LOG_DEBUG, "text match filter at level %d FIXME not implemented yet", this_rule_level);
+               }
+
+               else if (!strcasecmp(t[1], "time-range")) {
+                       syslog(LOG_DEBUG, "time range filter at level %d FIXME not implemented yet", this_rule_level);
                }
 
                ++f;
        }
 
+       syslog(LOG_DEBUG, "caldav: we reached the end of level %d , returning %d", apply_at_level, qual);
        return(qual);
 }
 
@@ -387,7 +455,7 @@ void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
 
                                // 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, "");