4 * Copyright (c) 1996-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "webserver.h"
27 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
29 icalcomponent *get_freebusy_for_user(char *who) {
32 StrBuf *serialized_fb = NewStrBuf();
33 icalcomponent *fb = NULL;
35 serv_printf("ICAL freebusy|%s", who);
36 serv_getln(buf, sizeof buf);
38 read_server_text(serialized_fb, &nLines);
41 if (serialized_fb == NULL) {
45 fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
46 FreeStrBuf(&serialized_fb);
56 * Check to see if two events overlap.
57 * (This function is used in both Citadel and WebCit. If you change it in
58 * one place, change it in the other. We should seriously consider moving
59 * this function upstream into libical.)
61 * Returns nonzero if they do overlap.
63 int ical_ctdl_is_overlap(
64 struct icaltimetype t1start,
65 struct icaltimetype t1end,
66 struct icaltimetype t2start,
67 struct icaltimetype t2end
70 if (icaltime_is_null_time(t1start)) return(0);
71 if (icaltime_is_null_time(t2start)) return(0);
73 /* if either event lacks end time, assume end = start */
74 if (icaltime_is_null_time(t1end))
75 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
77 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
79 * the end date is non-inclusive so adjust it by one
80 * day because our test is inclusive, note that a day is
81 * not too much because we are talking about all day
83 * if start = end we assume that nevertheless the whole
86 icaltime_adjust(&t1end, -1, 0, 0, 0);
90 if (icaltime_is_null_time(t2end))
91 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
93 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
94 icaltime_adjust(&t2end, -1, 0, 0, 0);
98 /* First, check for all-day events */
99 if (t1start.is_date || t2start.is_date) {
100 /* If event 1 ends before event 2 starts, we're in the clear. */
101 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
103 /* If event 2 ends before event 1 starts, we're also ok. */
104 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
109 /* lprintf (9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
110 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
111 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
114 /* Now check for overlaps using date *and* time. */
116 /* If event 1 ends before event 2 starts, we're in the clear. */
117 if (icaltime_compare(t1end, t2start) <= 0) return(0);
118 /* lprintf(9, "first passed\n"); */
120 /* If event 2 ends before event 1 starts, we're also ok. */
121 if (icaltime_compare(t2end, t1start) <= 0) return(0);
122 /* lprintf(9, "second passed\n"); */
124 /* Otherwise, they overlap. */
131 * Back end function for check_attendee_availability()
132 * This one checks an individual attendee against a supplied
133 * event start and end time. All these fields have already been
136 * attendee_string name of the attendee
137 * event_start start time of the event to check
138 * event_end end time of the event to check
140 * The result is placed in 'annotation'.
142 void check_individual_attendee(char *attendee_string,
143 struct icaltimetype event_start,
144 struct icaltimetype event_end,
147 icalcomponent *fbc = NULL;
148 icalcomponent *fb = NULL;
149 icalproperty *thisfb = NULL;
150 struct icalperiodtype period;
153 * Set to 'unknown' right from the beginning. Unless we learn
154 * something else, that's what we'll go with.
156 strcpy(annotation, _("availability unknown"));
158 fbc = get_freebusy_for_user(attendee_string);
164 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
165 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
167 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
168 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
170 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
174 /* Iterate through all FREEBUSY's looking for conflicts. */
177 strcpy(annotation, _("free"));
179 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
181 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
184 period = icalproperty_get_freebusy(thisfb);
185 if (ical_ctdl_is_overlap(period.start, period.end,
186 event_start, event_end)) {
187 strcpy(annotation, _("BUSY"));
193 icalcomponent_free(fbc);
200 * Check the availability of all attendees for an event (when possible)
201 * and annotate accordingly.
203 * vevent the event which should be compared with attendees calendar
205 void check_attendee_availability(icalcomponent *vevent) {
206 icalproperty *attendee = NULL;
207 icalproperty *dtstart_p = NULL;
208 icalproperty *dtend_p = NULL;
209 struct icaltimetype dtstart_t;
210 struct icaltimetype dtend_t;
211 char attendee_string[SIZ];
212 char annotated_attendee_string[SIZ];
213 char annotation[SIZ];
215 if (vevent == NULL) {
220 * If we're looking at a fully encapsulated VCALENDAR
221 * rather than a VEVENT component, attempt to use the first
222 * relevant VEVENT subcomponent. If there is none, the
223 * NULL returned by icalcomponent_get_first_component() will
224 * tell the next iteration of this function to create a
227 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
228 check_attendee_availability(
229 icalcomponent_get_first_component(
230 vevent, ICAL_VEVENT_COMPONENT
236 ical_dezonify(vevent); /**< Convert everything to UTC */
239 * Learn the start and end times.
241 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
242 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
244 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
245 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
248 * Iterate through attendees.
250 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
252 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
254 strcpy(attendee_string, icalproperty_get_attendee(attendee));
255 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
257 /** screen name or email address */
258 strcpy(attendee_string, &attendee_string[7]);
259 striplt(attendee_string);
261 check_individual_attendee(attendee_string,
265 /** Replace the attendee name with an annotated one. */
266 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
267 "MAILTO:%s (%s)", attendee_string, annotation);
268 icalproperty_set_attendee(attendee, annotated_attendee_string);