2 * Copyright (c) 1996-2010 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 as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "webserver.h"
25 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
27 icalcomponent *get_freebusy_for_user(char *who) {
30 StrBuf *serialized_fb = NewStrBuf();
31 icalcomponent *fb = NULL;
33 serv_printf("ICAL freebusy|%s", who);
34 serv_getln(buf, sizeof buf);
36 read_server_text(serialized_fb, &nLines);
39 if (serialized_fb == NULL) {
43 fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
44 FreeStrBuf(&serialized_fb);
54 * Check to see if two events overlap.
55 * (This function is used in both Citadel and WebCit. If you change it in
56 * one place, change it in the other. We should seriously consider moving
57 * this function upstream into libical.)
59 * Returns nonzero if they do overlap.
61 int ical_ctdl_is_overlap(
62 struct icaltimetype t1start,
63 struct icaltimetype t1end,
64 struct icaltimetype t2start,
65 struct icaltimetype t2end
68 if (icaltime_is_null_time(t1start)) return(0);
69 if (icaltime_is_null_time(t2start)) return(0);
71 /* if either event lacks end time, assume end = start */
72 if (icaltime_is_null_time(t1end))
73 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
75 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
77 * the end date is non-inclusive so adjust it by one
78 * day because our test is inclusive, note that a day is
79 * not too much because we are talking about all day
81 * if start = end we assume that nevertheless the whole
84 icaltime_adjust(&t1end, -1, 0, 0, 0);
88 if (icaltime_is_null_time(t2end))
89 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
91 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
92 icaltime_adjust(&t2end, -1, 0, 0, 0);
96 /* First, check for all-day events */
97 if (t1start.is_date || t2start.is_date) {
98 /* If event 1 ends before event 2 starts, we're in the clear. */
99 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
101 /* If event 2 ends before event 1 starts, we're also ok. */
102 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
107 /* syslog(9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
108 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
109 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
112 /* Now check for overlaps using date *and* time. */
114 /* If event 1 ends before event 2 starts, we're in the clear. */
115 if (icaltime_compare(t1end, t2start) <= 0) return(0);
116 /* syslog(9, "first passed\n"); */
118 /* If event 2 ends before event 1 starts, we're also ok. */
119 if (icaltime_compare(t2end, t1start) <= 0) return(0);
120 /* syslog(9, "second passed\n"); */
122 /* Otherwise, they overlap. */
129 * Back end function for check_attendee_availability()
130 * This one checks an individual attendee against a supplied
131 * event start and end time. All these fields have already been
134 * attendee_string name of the attendee
135 * event_start start time of the event to check
136 * event_end end time of the event to check
138 * The result is placed in 'annotation'.
140 void check_individual_attendee(char *attendee_string,
141 struct icaltimetype event_start,
142 struct icaltimetype event_end,
145 icalcomponent *fbc = NULL;
146 icalcomponent *fb = NULL;
147 icalproperty *thisfb = NULL;
148 struct icalperiodtype period;
151 * Set to 'unknown' right from the beginning. Unless we learn
152 * something else, that's what we'll go with.
154 strcpy(annotation, _("availability unknown"));
156 fbc = get_freebusy_for_user(attendee_string);
162 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
163 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
165 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
166 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
168 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
172 /* Iterate through all FREEBUSY's looking for conflicts. */
175 strcpy(annotation, _("free"));
177 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
179 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
182 period = icalproperty_get_freebusy(thisfb);
183 if (ical_ctdl_is_overlap(period.start, period.end,
184 event_start, event_end)) {
185 strcpy(annotation, _("BUSY"));
191 icalcomponent_free(fbc);
198 * Check the availability of all attendees for an event (when possible)
199 * and annotate accordingly.
201 * vevent the event which should be compared with attendees calendar
203 void check_attendee_availability(icalcomponent *vevent) {
204 icalproperty *attendee = NULL;
205 icalproperty *dtstart_p = NULL;
206 icalproperty *dtend_p = NULL;
207 struct icaltimetype dtstart_t;
208 struct icaltimetype dtend_t;
209 char attendee_string[SIZ];
210 char annotated_attendee_string[SIZ];
211 char annotation[SIZ];
214 if (vevent == NULL) {
219 * If we're looking at a fully encapsulated VCALENDAR
220 * rather than a VEVENT component, attempt to use the first
221 * relevant VEVENT subcomponent. If there is none, the
222 * NULL returned by icalcomponent_get_first_component() will
223 * tell the next iteration of this function to create a
226 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
227 check_attendee_availability(
228 icalcomponent_get_first_component(
229 vevent, ICAL_VEVENT_COMPONENT
235 ical_dezonify(vevent); /**< Convert everything to UTC */
238 * Learn the start and end times.
240 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
241 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
243 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
244 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
247 * Iterate through attendees.
249 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
251 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
252 ch = icalproperty_get_attendee(attendee);
253 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
255 /** screen name or email address */
256 safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
257 striplt(attendee_string);
259 check_individual_attendee(attendee_string,
263 /** Replace the attendee name with an annotated one. */
264 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
265 "MAILTO:%s (%s)", attendee_string, annotation);
266 icalproperty_set_attendee(attendee, annotated_attendee_string);