Back to index

webcit  8.12-dfsg
availability.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996-2012 by the citadel.org team
00003  *
00004  * This program is open source software.  You can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License, version 3.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  */
00012 
00013 
00014 #include "webcit.h"
00015 #include "webserver.h"
00016 #include "calendar.h"
00017 
00018 /*
00019  * Utility function to fetch a VFREEBUSY type of thing for any specified user.
00020  */
00021 icalcomponent *get_freebusy_for_user(char *who) {
00022        long nLines;
00023        char buf[SIZ];
00024        StrBuf *serialized_fb = NewStrBuf();
00025        icalcomponent *fb = NULL;
00026 
00027        serv_printf("ICAL freebusy|%s", who);
00028        serv_getln(buf, sizeof buf);
00029        if (buf[0] == '1') {
00030               read_server_text(serialized_fb, &nLines);
00031        }
00032 
00033        if (serialized_fb == NULL) {
00034               return NULL;
00035        }
00036        
00037        fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
00038        FreeStrBuf(&serialized_fb);
00039        if (fb == NULL) {
00040               return NULL;
00041        }
00042 
00043        return(fb);
00044 }
00045 
00046 
00047 /*
00048  * Check to see if two events overlap.  
00049  * (This function is used in both Citadel and WebCit.  If you change it in
00050  * one place, change it in the other.  We should seriously consider moving
00051  * this function upstream into libical.)
00052  *
00053  * Returns nonzero if they do overlap.
00054  */
00055 int ical_ctdl_is_overlap(
00056                      struct icaltimetype t1start,
00057                      struct icaltimetype t1end,
00058                      struct icaltimetype t2start,
00059                      struct icaltimetype t2end
00060 ) {
00061 
00062        if (icaltime_is_null_time(t1start)) return(0);
00063        if (icaltime_is_null_time(t2start)) return(0);
00064 
00065        /* if either event lacks end time, assume end = start */
00066        if (icaltime_is_null_time(t1end))
00067               memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
00068        else {
00069               if (t1end.is_date && icaltime_compare(t1start, t1end)) {
00070                         /*
00071                          * the end date is non-inclusive so adjust it by one
00072                          * day because our test is inclusive, note that a day is
00073                          * not too much because we are talking about all day
00074                          * events
00075                       * if start = end we assume that nevertheless the whole
00076                       * day is meant
00077                          */
00078                      icaltime_adjust(&t1end, -1, 0, 0, 0);     
00079               }
00080        }
00081 
00082        if (icaltime_is_null_time(t2end))
00083               memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
00084        else {
00085               if (t2end.is_date && icaltime_compare(t2start, t2end)) {
00086                      icaltime_adjust(&t2end, -1, 0, 0, 0);     
00087               }
00088        }
00089 
00090        /* First, check for all-day events */
00091        if (t1start.is_date || t2start.is_date) {
00092               /* If event 1 ends before event 2 starts, we're in the clear. */
00093               if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
00094 
00095               /* If event 2 ends before event 1 starts, we're also ok. */
00096               if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
00097 
00098               return(1);
00099        }
00100 
00101        /* syslog(9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
00102               t1start.hour, t1start.minute, t1end.hour, t1end.minute,
00103               t2start.hour, t2start.minute, t2end.hour, t2end.minute);
00104        */
00105 
00106        /* Now check for overlaps using date *and* time. */
00107 
00108        /* If event 1 ends before event 2 starts, we're in the clear. */
00109        if (icaltime_compare(t1end, t2start) <= 0) return(0);
00110        /* syslog(9, "first passed\n"); */
00111 
00112        /* If event 2 ends before event 1 starts, we're also ok. */
00113        if (icaltime_compare(t2end, t1start) <= 0) return(0);
00114        /* syslog(9, "second passed\n"); */
00115 
00116        /* Otherwise, they overlap. */
00117        return(1);
00118 }
00119 
00120 
00121 
00122 /*
00123  * Back end function for check_attendee_availability()
00124  * This one checks an individual attendee against a supplied
00125  * event start and end time.  All these fields have already been
00126  * broken out.  
00127  *
00128  * attendee_string   name of the attendee
00129  * event_start              start time of the event to check
00130  * event_end         end time of the event to check
00131  *
00132  * The result is placed in 'annotation'.
00133  */
00134 void check_individual_attendee(char *attendee_string,
00135                             struct icaltimetype event_start,
00136                             struct icaltimetype event_end,
00137                             char *annotation) {
00138 
00139        icalcomponent *fbc = NULL;
00140        icalcomponent *fb = NULL;
00141        icalproperty *thisfb = NULL;
00142        struct icalperiodtype period;
00143 
00144        /*
00145         * Set to 'unknown' right from the beginning.  Unless we learn
00146         * something else, that's what we'll go with.
00147         */
00148        strcpy(annotation, _("availability unknown"));
00149 
00150        fbc = get_freebusy_for_user(attendee_string);
00151        if (fbc == NULL) {
00152               return;
00153        }
00154 
00155        /*
00156         * Make sure we're looking at a VFREEBUSY by itself.  What we're probably
00157         * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
00158         */
00159        if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
00160               fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
00161        }
00162        else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
00163               fb = fbc;
00164        }
00165 
00166        /* Iterate through all FREEBUSY's looking for conflicts. */
00167        if (fb != NULL) {
00168 
00169               strcpy(annotation, _("free"));
00170 
00171               for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
00172                   thisfb != NULL;
00173                   thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
00174 
00176                      period = icalproperty_get_freebusy(thisfb);
00177                      if (ical_ctdl_is_overlap(period.start, period.end,
00178                         event_start, event_end)) {
00179                             strcpy(annotation, _("BUSY"));
00180                      }
00181 
00182               }
00183        }
00184 
00185        icalcomponent_free(fbc);
00186 }
00187 
00188 
00189 
00190 
00191 /*
00192  * Check the availability of all attendees for an event (when possible)
00193  * and annotate accordingly.
00194  *
00195  * vevent     the event which should be compared with attendees calendar
00196  */
00197 void check_attendee_availability(icalcomponent *vevent) {
00198        icalproperty *attendee = NULL;
00199        icalproperty *dtstart_p = NULL;
00200        icalproperty *dtend_p = NULL;
00201        struct icaltimetype dtstart_t;
00202        struct icaltimetype dtend_t;
00203        char attendee_string[SIZ];
00204        char annotated_attendee_string[SIZ];
00205        char annotation[SIZ];
00206        const char *ch;
00207 
00208        if (vevent == NULL) {
00209               return;
00210        }
00211 
00212        /*
00213         * If we're looking at a fully encapsulated VCALENDAR
00214         * rather than a VEVENT component, attempt to use the first
00215         * relevant VEVENT subcomponent.  If there is none, the
00216         * NULL returned by icalcomponent_get_first_component() will
00217         * tell the next iteration of this function to create a
00218         * new one.
00219         */
00220        if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
00221               check_attendee_availability(
00222                      icalcomponent_get_first_component(
00223                             vevent, ICAL_VEVENT_COMPONENT
00224                      )
00225               );
00226               return;
00227        }
00228 
00229        ical_dezonify(vevent);             
00231        /*
00232         * Learn the start and end times.
00233         */
00234        dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
00235        if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
00236 
00237        dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
00238        if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
00239 
00240        /*
00241         * Iterate through attendees.
00242         */
00243        for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
00244            attendee != NULL;
00245            attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
00246               ch = icalproperty_get_attendee(attendee);
00247               if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
00248 
00250                      safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
00251                      striplt(attendee_string);
00252 
00253                      check_individual_attendee(attendee_string,
00254                                           dtstart_t, dtend_t,
00255                                           annotation);
00256 
00258                      snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
00259                             "MAILTO:%s (%s)", attendee_string, annotation);
00260                      icalproperty_set_attendee(attendee, annotated_attendee_string);
00261 
00262               }
00263        }
00264 
00265 }
00266