XML is like violence: if it doesn't work, you're not using enough.
authorArt Cancro <ajc@citadel.org>
Mon, 26 Feb 2024 22:37:20 +0000 (17:37 -0500)
committerArt Cancro <ajc@citadel.org>
Mon, 26 Feb 2024 22:37:20 +0000 (17:37 -0500)
webcit-ng/server/caldav_reports.c
webcit-ng/server/webcit.h

index ef9072829d65d070f56a543e909ffe163db2543b..76a0ee43d1438a9c65999552254827f3b752d6b9 100644 (file)
@@ -274,10 +274,11 @@ 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) ) {
 
@@ -295,7 +296,16 @@ int caldav_apply_filters(void *cal, Array *filters) {
 
                // 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
+               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!
@@ -304,15 +314,46 @@ int caldav_apply_filters(void *cal, Array *filters) {
                                return(0);
                        }
 
-                       // Root element IS a component and the root filter is "comp-filter" -- see if it matches the requested type
+                       // 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)
+                               //&& (level == 0)
                                && (!strcasecmp(t[2], "name"))
                        ) {
-                               if (icalcomponent_isa(cal) != icalcomponent_string_to_kind(t[3]) ) {
-                                       syslog(LOG_DEBUG, "caldav: root component is <%s>, looking for <%s>, rejecting",
+                               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);
                                }
                        }
@@ -338,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);
 }
 
@@ -404,7 +446,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, "");
 
index 92738063bfa7ff1302f109981904adee34da7dfb..03efc5e816eaeed44340d7d85ec176c62ca45733 100644 (file)
@@ -157,8 +157,6 @@ void caldav_xml_chardata(void *, const XML_Char *, int);
 StrBuf *fetch_ical(struct ctdlsession *, long);
 void cal_multiget_out(long, StrBuf *, StrBuf *, StrBuf *);
 void caldav_report_one_item(struct http_transaction *, struct ctdlsession *, StrBuf *, StrBuf *);
-int caldav_apply_one_filter(void *, char *);
-int caldav_apply_filters(void *, Array *);
 void caldav_report(struct http_transaction *, struct ctdlsession *);
 
 // ctdlclient.c