4 * Check attendee availability for scheduling a meeting.
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
31 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
36 * Utility function to fetch a VFREEBUSY type of thing for
39 icalcomponent *get_freebusy_for_user(char *who) {
41 char *serialized_fb = NULL;
42 icalcomponent *fb = NULL;
44 serv_printf("ICAL freebusy|%s", who);
45 serv_getln(buf, sizeof buf);
47 serialized_fb = read_server_text();
50 if (serialized_fb == NULL) {
54 fb = icalcomponent_new_from_string(serialized_fb);
67 * Check to see if two events overlap. Returns nonzero if they do.
68 * (This function is used in both Citadel and WebCit. If you change it in
69 * one place, change it in the other. Better yet, put it in a library.)
71 int ical_ctdl_is_overlap(
72 struct icaltimetype t1start,
73 struct icaltimetype t1end,
74 struct icaltimetype t2start,
75 struct icaltimetype t2end
78 if (icaltime_is_null_time(t1start)) return(0);
79 if (icaltime_is_null_time(t2start)) return(0);
81 /* First, check for all-day events */
82 if (t1start.is_date) {
83 if (!icaltime_compare_date_only(t1start, t2start)) {
86 if (!icaltime_is_null_time(t2end)) {
87 if (!icaltime_compare_date_only(t1start, t2end)) {
93 if (t2start.is_date) {
94 if (!icaltime_compare_date_only(t2start, t1start)) {
97 if (!icaltime_is_null_time(t1end)) {
98 if (!icaltime_compare_date_only(t2start, t1end)) {
104 /* Now check for overlaps using date *and* time. */
106 /* First, bail out if either event 1 or event 2 is missing end time. */
107 if (icaltime_is_null_time(t1end)) return(0);
108 if (icaltime_is_null_time(t2end)) return(0);
110 /* If event 1 ends before event 2 starts, we're in the clear. */
111 if (icaltime_compare(t1end, t2start) <= 0) return(0);
113 /* If event 2 ends before event 1 starts, we're also ok. */
114 if (icaltime_compare(t2end, t1start) <= 0) return(0);
116 /* Otherwise, they overlap. */
123 * Back end function for check_attendee_availability()
124 * This one checks an individual attendee against a supplied
125 * event start and end time. All these fields have already been
126 * broken out. The result is placed in 'annotation'.
128 void check_individual_attendee(char *attendee_string,
129 struct icaltimetype event_start,
130 struct icaltimetype event_end,
133 icalcomponent *fbc = NULL;
134 icalcomponent *fb = NULL;
135 icalproperty *thisfb = NULL;
136 struct icalperiodtype period;
138 /* Set to 'unknown' right from the beginning. Unless we learn
139 * something else, that's what we'll go with.
141 strcpy(annotation, "availability unknown");
143 fbc = get_freebusy_for_user(attendee_string);
148 /* Make sure we're looking at a VFREEBUSY by itself. What we're probably
149 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
151 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
152 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
154 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
158 /* Iterate through all FREEBUSY's looking for conflicts. */
161 strcpy(annotation, "free");
163 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
165 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
168 period = icalproperty_get_freebusy(thisfb);
169 if (ical_ctdl_is_overlap(period.start, period.end,
170 event_start, event_end)) {
171 strcpy(annotation, "BUSY");
177 icalcomponent_free(fbc);
184 * Check the availability of all attendees for an event (when possible)
185 * and annotate accordingly.
187 void check_attendee_availability(icalcomponent *vevent) {
188 icalproperty *attendee = NULL;
189 icalproperty *dtstart_p = NULL;
190 icalproperty *dtend_p = NULL;
191 struct icaltimetype dtstart_t;
192 struct icaltimetype dtend_t;
193 char attendee_string[SIZ];
194 char annotated_attendee_string[SIZ];
195 char annotation[SIZ];
197 if (vevent == NULL) {
201 /* If we're looking at a fully encapsulated VCALENDAR
202 * rather than a VEVENT component, attempt to use the first
203 * relevant VEVENT subcomponent. If there is none, the
204 * NULL returned by icalcomponent_get_first_component() will
205 * tell the next iteration of this function to create a
208 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
209 check_attendee_availability(
210 icalcomponent_get_first_component(
211 vevent, ICAL_VEVENT_COMPONENT
217 ical_dezonify(vevent); /* Convert everything to UTC */
220 * Learn the start and end times.
222 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
223 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
225 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
226 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
229 * Iterate through attendees.
231 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
233 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
235 strcpy(attendee_string, icalproperty_get_attendee(attendee));
236 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
238 /* screen name or email address */
239 strcpy(attendee_string, &attendee_string[7]);
240 striplt(attendee_string);
242 check_individual_attendee(attendee_string,
246 /* Replace the attendee name with an annotated one. */
247 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
248 "MAILTO:%s (%s)", attendee_string, annotation);
249 icalproperty_set_attendee(attendee, annotated_attendee_string);
257 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */