]> code.citadel.org Git - citadel.git/blobdiff - webcit/availability.c
* serv_func.c: added utility function read_server_text()
[citadel.git] / webcit / availability.c
index ceb806e9dd00ca2afbea6475953138e77a98b983..8fa41bf7f3532160311856424df15a882cb0c432 100644 (file)
 
 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
 
+
+
 /*
- * Display an event by itself (for editing)
+ * Utility function to fetch a VFREEBUSY type of thing for
+ * any specified user.
+ */
+icalcomponent *get_freebusy_for_user(char *who) {
+       char buf[SIZ];
+       char *serialized_fb = NULL;
+       icalcomponent *fb = NULL;
+
+       serv_printf("ICAL freebusy|%s", who);
+       serv_gets(buf);
+       if (buf[0] == '1') {
+               serialized_fb = read_server_text();
+       }
+
+       if (serialized_fb == NULL) {
+               return NULL;
+       }
+       
+       fb = icalcomponent_new_from_string(serialized_fb);
+       free(serialized_fb);
+       if (fb == NULL) {
+               return NULL;
+       }
+
+       return fb;
+}
+
+
+
+/*
+ * Back end function for check_attendee_availability()
+ * This one checks an individual attendee against a supplied
+ * event start and end time.  All these fields have already been
+ * broken out.  The result is placed in 'annotation'.
+ */
+void check_individual_attendee(char *attendee_string,
+                               struct icaltimetype event_start,
+                               struct icaltimetype event_end,
+                               char *annotation) {
+
+       icalcomponent *fbc = NULL;
+       icalcomponent *fb = NULL;
+       icalproperty *thisfb = NULL;
+
+       /* Set to 'unknown' right from the beginning.  Unless we learn
+        * something else, that's what we'll go with.
+        */
+       strcpy(annotation, "availability unknown");
+
+       fbc = get_freebusy_for_user(attendee_string);
+       if (fbc == NULL) {
+               return;
+       }
+
+       /* Make sure we're looking at a VFREEBUSY by itself.  What we're probably
+        * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
+        */
+       if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
+               fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
+       }
+       else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
+               fb = fbc;
+       }
+
+       /* Iterate through all FREEBUSY's looking for conflicts. */
+       if (fb != NULL) {
+
+               strcpy(annotation, "free");
+
+               for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
+                   thisfb != NULL;
+                   thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
+       
+                       /* FIXME ... do the check */
+
+               }
+       }
+
+       icalcomponent_free(fbc);
+}
+
+
+
+/*
+ * Check the availability of all attendees for an event (when possible)
+ * and annotate accordingly.
  */
 void check_attendee_availability(icalcomponent *vevent) {
        icalproperty *attendee = NULL;
+       icalproperty *dtstart_p = NULL;
+       icalproperty *dtend_p = NULL;
+       struct icaltimetype dtstart_t;
+       struct icaltimetype dtend_t;
        char attendee_string[SIZ];
+       char annotated_attendee_string[SIZ];
+       char annotation[SIZ];
 
        if (vevent == NULL) {
                return;
@@ -57,9 +150,17 @@ void check_attendee_availability(icalcomponent *vevent) {
                return;
        }
 
+       /*
+        * Learn the start and end times.
+        */
+       dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
+       if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
+
+       dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
+       if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
 
        /*
-        * Iterate through attendees.  FIXME do something useful.
+        * Iterate through attendees.
         */
        for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
            attendee != NULL;
@@ -72,11 +173,15 @@ void check_attendee_availability(icalcomponent *vevent) {
                        strcpy(attendee_string, &attendee_string[7]);
                        striplt(attendee_string);
 
-                       /* FIXME do something with attendee_string */
-                       lprintf(9, "FIXME with <%s>\n", attendee_string);
+                       check_individual_attendee(attendee_string,
+                                               dtstart_t, dtend_t,
+                                               annotation);
+
+                       /* Replace the attendee name with an annotated one. */
+                       snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
+                               "MAILTO:%s (%s)", attendee_string, annotation);
+                       icalproperty_set_attendee(attendee, annotated_attendee_string);
 
-                       /* participant status 
-                       partstat_as_string(buf, attendee); */
                }
        }