3 * Copyright (c) 1996-2012 by the citadel.org team
5 * This program is open source software. You can redistribute it and/or
6 * modify it under the terms of the GNU General Public License, version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include "webserver.h"
20 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
22 icalcomponent *get_freebusy_for_user(char *who) {
25 StrBuf *serialized_fb = NewStrBuf();
26 icalcomponent *fb = NULL;
28 serv_printf("ICAL freebusy|%s", who);
29 serv_getln(buf, sizeof buf);
31 read_server_text(serialized_fb, &nLines);
34 if (serialized_fb == NULL) {
38 fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
39 FreeStrBuf(&serialized_fb);
49 * Check to see if two events overlap.
50 * (This function is used in both Citadel and WebCit. If you change it in
51 * one place, change it in the other. We should seriously consider moving
52 * this function upstream into libical.)
54 * Returns nonzero if they do overlap.
56 int ical_ctdl_is_overlap(struct icaltimetype t1start,
57 struct icaltimetype t1end, struct icaltimetype t2start, struct icaltimetype t2end) {
59 if (icaltime_is_null_time(t1start))
61 if (icaltime_is_null_time(t2start))
64 /* if either event lacks end time, assume end = start */
65 if (icaltime_is_null_time(t1end))
66 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
68 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
70 * the end date is non-inclusive so adjust it by one
71 * day because our test is inclusive, note that a day is
72 * not too much because we are talking about all day
74 * if start = end we assume that nevertheless the whole
77 icaltime_adjust(&t1end, -1, 0, 0, 0);
81 if (icaltime_is_null_time(t2end))
82 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
84 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
85 icaltime_adjust(&t2end, -1, 0, 0, 0);
89 /* First, check for all-day events */
90 if (t1start.is_date || t2start.is_date) {
91 /* If event 1 ends before event 2 starts, we're in the clear. */
92 if (icaltime_compare_date_only(t1end, t2start) < 0)
95 /* If event 2 ends before event 1 starts, we're also ok. */
96 if (icaltime_compare_date_only(t2end, t1start) < 0)
102 /* syslog(LOG_DEBUG, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
103 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
104 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
107 /* Now check for overlaps using date *and* time. */
109 /* If event 1 ends before event 2 starts, we're in the clear. */
110 if (icaltime_compare(t1end, t2start) <= 0)
112 /* syslog(LOG_DEBUG, "first passed\n"); */
114 /* If event 2 ends before event 1 starts, we're also ok. */
115 if (icaltime_compare(t2end, t1start) <= 0)
117 /* syslog(LOG_DEBUG, "second passed\n"); */
119 /* Otherwise, they overlap. */
126 * Back end function for check_attendee_availability()
127 * This one checks an individual attendee against a supplied
128 * event start and end time. All these fields have already been
131 * attendee_string name of the attendee
132 * event_start start time of the event to check
133 * event_end end time of the event to check
135 * The result is placed in 'annotation'.
137 void check_individual_attendee(char *attendee_string,
138 struct icaltimetype event_start, struct icaltimetype event_end, char *annotation) {
140 icalcomponent *fbc = NULL;
141 icalcomponent *fb = NULL;
142 icalproperty *thisfb = NULL;
143 struct icalperiodtype period;
146 * Set to 'unknown' right from the beginning. Unless we learn
147 * something else, that's what we'll go with.
149 strcpy(annotation, _("availability unknown"));
151 fbc = get_freebusy_for_user(attendee_string);
157 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
158 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
160 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
161 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
163 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
167 /* Iterate through all FREEBUSY's looking for conflicts. */
170 strcpy(annotation, _("free"));
172 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
173 thisfb != NULL; 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, event_start, event_end)) {
178 strcpy(annotation, _("BUSY"));
184 icalcomponent_free(fbc);
191 * Check the availability of all attendees for an event (when possible)
192 * and annotate accordingly.
194 * vevent the event which should be compared with attendees calendar
196 void check_attendee_availability(icalcomponent * vevent) {
197 icalproperty *attendee = NULL;
198 icalproperty *dtstart_p = NULL;
199 icalproperty *dtend_p = NULL;
200 struct icaltimetype dtstart_t;
201 struct icaltimetype dtend_t;
202 char attendee_string[SIZ];
203 char annotated_attendee_string[SIZ];
204 char annotation[SIZ];
207 if (vevent == NULL) {
212 * If we're looking at a fully encapsulated VCALENDAR
213 * rather than a VEVENT component, attempt to use the first
214 * relevant VEVENT subcomponent. If there is none, the
215 * NULL returned by icalcomponent_get_first_component() will
216 * tell the next iteration of this function to create a
219 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
220 check_attendee_availability(icalcomponent_get_first_component(vevent, ICAL_VEVENT_COMPONENT)
225 ical_dezonify(vevent); /**< Convert everything to UTC */
228 * Learn the start and end times.
230 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
231 if (dtstart_p != NULL)
232 dtstart_t = icalproperty_get_dtstart(dtstart_p);
234 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
236 dtend_t = icalproperty_get_dtend(dtend_p);
239 * Iterate through attendees.
241 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
242 attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
243 ch = icalproperty_get_attendee(attendee);
244 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
246 /** screen name or email address */
247 safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
248 striplt(attendee_string);
250 check_individual_attendee(attendee_string, dtstart_t, dtend_t, annotation);
252 /** Replace the attendee name with an annotated one. */
253 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
254 "MAILTO:%s (%s)", attendee_string, annotation);
255 icalproperty_set_attendee(attendee, annotated_attendee_string);