2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
15 #include "webserver.h"
19 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
21 icalcomponent *get_freebusy_for_user(char *who) {
24 StrBuf *serialized_fb = NewStrBuf();
25 icalcomponent *fb = NULL;
27 serv_printf("ICAL freebusy|%s", who);
28 serv_getln(buf, sizeof buf);
30 read_server_text(serialized_fb, &nLines);
33 if (serialized_fb == NULL) {
37 fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
38 FreeStrBuf(&serialized_fb);
48 * Check to see if two events overlap.
49 * (This function is used in both Citadel and WebCit. If you change it in
50 * one place, change it in the other. We should seriously consider moving
51 * this function upstream into libical.)
53 * Returns nonzero if they do overlap.
55 int ical_ctdl_is_overlap(
56 struct icaltimetype t1start,
57 struct icaltimetype t1end,
58 struct icaltimetype t2start,
59 struct icaltimetype t2end
62 if (icaltime_is_null_time(t1start)) return(0);
63 if (icaltime_is_null_time(t2start)) return(0);
65 /* if either event lacks end time, assume end = start */
66 if (icaltime_is_null_time(t1end))
67 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
69 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
71 * the end date is non-inclusive so adjust it by one
72 * day because our test is inclusive, note that a day is
73 * not too much because we are talking about all day
75 * if start = end we assume that nevertheless the whole
78 icaltime_adjust(&t1end, -1, 0, 0, 0);
82 if (icaltime_is_null_time(t2end))
83 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
85 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
86 icaltime_adjust(&t2end, -1, 0, 0, 0);
90 /* First, check for all-day events */
91 if (t1start.is_date || t2start.is_date) {
92 /* If event 1 ends before event 2 starts, we're in the clear. */
93 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
95 /* If event 2 ends before event 1 starts, we're also ok. */
96 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
101 /* syslog(9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
102 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
103 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
106 /* Now check for overlaps using date *and* time. */
108 /* If event 1 ends before event 2 starts, we're in the clear. */
109 if (icaltime_compare(t1end, t2start) <= 0) return(0);
110 /* syslog(9, "first passed\n"); */
112 /* If event 2 ends before event 1 starts, we're also ok. */
113 if (icaltime_compare(t2end, t1start) <= 0) return(0);
114 /* syslog(9, "second passed\n"); */
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
128 * attendee_string name of the attendee
129 * event_start start time of the event to check
130 * event_end end time of the event to check
132 * The result is placed in 'annotation'.
134 void check_individual_attendee(char *attendee_string,
135 struct icaltimetype event_start,
136 struct icaltimetype event_end,
139 icalcomponent *fbc = NULL;
140 icalcomponent *fb = NULL;
141 icalproperty *thisfb = NULL;
142 struct icalperiodtype period;
145 * Set to 'unknown' right from the beginning. Unless we learn
146 * something else, that's what we'll go with.
148 strcpy(annotation, _("availability unknown"));
150 fbc = get_freebusy_for_user(attendee_string);
156 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
157 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
159 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
160 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
162 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
166 /* Iterate through all FREEBUSY's looking for conflicts. */
169 strcpy(annotation, _("free"));
171 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
173 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
176 period = icalproperty_get_freebusy(thisfb);
177 if (ical_ctdl_is_overlap(period.start, period.end,
178 event_start, event_end)) {
179 strcpy(annotation, _("BUSY"));
185 icalcomponent_free(fbc);
192 * Check the availability of all attendees for an event (when possible)
193 * and annotate accordingly.
195 * vevent the event which should be compared with attendees calendar
197 void check_attendee_availability(icalcomponent *vevent) {
198 icalproperty *attendee = NULL;
199 icalproperty *dtstart_p = NULL;
200 icalproperty *dtend_p = NULL;
201 struct icaltimetype dtstart_t;
202 struct icaltimetype dtend_t;
203 char attendee_string[SIZ];
204 char annotated_attendee_string[SIZ];
205 char annotation[SIZ];
208 if (vevent == NULL) {
213 * If we're looking at a fully encapsulated VCALENDAR
214 * rather than a VEVENT component, attempt to use the first
215 * relevant VEVENT subcomponent. If there is none, the
216 * NULL returned by icalcomponent_get_first_component() will
217 * tell the next iteration of this function to create a
220 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
221 check_attendee_availability(
222 icalcomponent_get_first_component(
223 vevent, ICAL_VEVENT_COMPONENT
229 ical_dezonify(vevent); /**< Convert everything to UTC */
232 * Learn the start and end times.
234 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
235 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
237 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
238 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
241 * Iterate through attendees.
243 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
245 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
246 ch = icalproperty_get_attendee(attendee);
247 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
249 /** screen name or email address */
250 safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
251 striplt(attendee_string);
253 check_individual_attendee(attendee_string,
257 /** Replace the attendee name with an annotated one. */
258 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
259 "MAILTO:%s (%s)", attendee_string, annotation);
260 icalproperty_set_attendee(attendee, annotated_attendee_string);