6 * \defgroup CalendarAv Check attendee availability for scheduling a meeting.
13 #include "webserver.h"
15 /** only available if we have calendaring */
16 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
21 * \brief verify users avaiability
22 * Utility function to fetch a VFREEBUSY type of thing for
24 * \param who string of the user to search
26 icalcomponent *get_freebusy_for_user(char *who) {
28 char *serialized_fb = NULL;
29 icalcomponent *fb = NULL;
31 serv_printf("ICAL freebusy|%s", who);
32 serv_getln(buf, sizeof buf);
34 serialized_fb = read_server_text();
37 if (serialized_fb == NULL) {
41 fb = icalcomponent_new_from_string(serialized_fb);
51 /* TODO: temporary copy from libical, apply this patch to libical... */
52 int webcit_icaltime_compare(const struct icaltimetype a_in, const struct icaltimetype b_in)
54 struct icaltimetype a, b;
56 a = icaltime_convert_to_zone(a_in, icaltimezone_get_utc_timezone());
57 b = icaltime_convert_to_zone(b_in, icaltimezone_get_utc_timezone());
61 else if (a.year < b.year)
64 else if (a.month > b.month)
66 else if (a.month < b.month)
69 else if (a.day > b.day)
71 else if (a.day < b.day)
74 /* if both are dates, we are done */
75 if (a.is_date && b.is_date)
78 /* else, if only one is a date (and we already know the date part is equal),
79 then the other is greater */
85 else if (a.hour > b.hour)
87 else if (a.hour < b.hour)
90 else if (a.minute > b.minute)
92 else if (a.minute < b.minute)
95 else if (a.second > b.second)
97 else if (a.second < b.second)
105 * \brief Check if dates are overlapping
106 * Check to see if two events overlap.
107 * (This function is used in both Citadel and WebCit. If you change it in
108 * one place, change it in the other. Better yet, put it in a library.)
109 * \param t1start date one start
110 * \param t1end date one end
111 * \param t2start date one start
112 * \param t2end date two end
113 * \returns nonzero if they do.
115 int ical_ctdl_is_overlap(
116 struct icaltimetype t1start,
117 struct icaltimetype t1end,
118 struct icaltimetype t2start,
119 struct icaltimetype t2end
122 if (icaltime_is_null_time(t1start)) return(0);
123 if (icaltime_is_null_time(t2start)) return(0);
125 /** First, check for all-day events */
126 if (t1start.is_date) {
127 if (!icaltime_compare_date_only(t1start, t2start)) {
130 if (!icaltime_is_null_time(t2end)) {
131 if (!icaltime_compare_date_only(t1start, t2end)) {
137 if (t2start.is_date) {
138 if (!icaltime_compare_date_only(t2start, t1start)) {
141 if (!icaltime_is_null_time(t1end)) {
142 if (!icaltime_compare_date_only(t2start, t1end)) {
148 lprintf (2,"Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n", t1start.hour, t1start.minute, t1end.hour, t1end.minute, t2start.hour, t2start.minute, t2end.hour, t2end.minute);// TODO: remove me.
150 /** Now check for overlaps using date *and* time. */
152 /** First, bail out if either event 1 or event 2 is missing end time. */
153 if (icaltime_is_null_time(t1end)) return(0);
154 if (icaltime_is_null_time(t2end)) return(0);
156 /** If event 1 ends before event 2 starts, we're in the clear. */
157 if (webcit_icaltime_compare(t1end, t2start) <= 0) return(0);
158 lprintf(2,"first pased \n");
159 /** If event 2 ends before event 1 starts, we're also ok. */
160 if (webcit_icaltime_compare(t2end, t1start) <= 0) return(0);
161 lprintf(2,"second pased \n");
163 /** Otherwise, they overlap. */
170 * \brief dig availability on citserver
171 * Back end function for check_attendee_availability()
172 * This one checks an individual attendee against a supplied
173 * event start and end time. All these fields have already been
175 * \param attendee_string name of the attendee
176 * \param event_start starttime of the event to check
177 * \param event_end endtime of the event to check
178 * \return The result is placed in 'annotation'.
180 void check_individual_attendee(char *attendee_string,
181 struct icaltimetype event_start,
182 struct icaltimetype event_end,
185 icalcomponent *fbc = NULL;
186 icalcomponent *fb = NULL;
187 icalproperty *thisfb = NULL;
188 struct icalperiodtype period;
191 * Set to 'unknown' right from the beginning. Unless we learn
192 * something else, that's what we'll go with.
194 strcpy(annotation, _("availability unknown"));
196 fbc = get_freebusy_for_user(attendee_string);
202 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
203 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
205 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
206 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
208 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
212 /** Iterate through all FREEBUSY's looking for conflicts. */
215 strcpy(annotation, _("free"));
217 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
219 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
222 period = icalproperty_get_freebusy(thisfb);
223 if (ical_ctdl_is_overlap(period.start, period.end,
224 event_start, event_end)) {
225 strcpy(annotation, _("BUSY"));
231 icalcomponent_free(fbc);
238 * \brief check attendees availability
239 * Check the availability of all attendees for an event (when possible)
240 * and annotate accordingly.
241 * \param vevent the event which should be compared with attendees calendar
243 void check_attendee_availability(icalcomponent *vevent) {
244 icalproperty *attendee = NULL;
245 icalproperty *dtstart_p = NULL;
246 icalproperty *dtend_p = NULL;
247 struct icaltimetype dtstart_t;
248 struct icaltimetype dtend_t;
249 char attendee_string[SIZ];
250 char annotated_attendee_string[SIZ];
251 char annotation[SIZ];
253 if (vevent == NULL) {
258 * If we're looking at a fully encapsulated VCALENDAR
259 * rather than a VEVENT component, attempt to use the first
260 * relevant VEVENT subcomponent. If there is none, the
261 * NULL returned by icalcomponent_get_first_component() will
262 * tell the next iteration of this function to create a
265 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
266 check_attendee_availability(
267 icalcomponent_get_first_component(
268 vevent, ICAL_VEVENT_COMPONENT
274 ical_dezonify(vevent); /**< Convert everything to UTC */
277 * Learn the start and end times.
279 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
280 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
282 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
283 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
286 * Iterate through attendees.
288 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
290 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
292 strcpy(attendee_string, icalproperty_get_attendee(attendee));
293 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
295 /** screen name or email address */
296 strcpy(attendee_string, &attendee_string[7]);
297 striplt(attendee_string);
299 check_individual_attendee(attendee_string,
303 /** Replace the attendee name with an annotated one. */
304 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
305 "MAILTO:%s (%s)", attendee_string, annotation);
306 icalproperty_set_attendee(attendee, annotated_attendee_string);
314 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */