11 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
13 icalcomponent *get_freebusy_for_user(char *who) {
15 char *serialized_fb = NULL;
16 icalcomponent *fb = NULL;
18 serv_printf("ICAL freebusy|%s", who);
19 serv_getln(buf, sizeof buf);
21 serialized_fb = read_server_text();
24 if (serialized_fb == NULL) {
28 fb = icalcomponent_new_from_string(serialized_fb);
39 * Check to see if two events overlap.
40 * (This function is used in both Citadel and WebCit. If you change it in
41 * one place, change it in the other. We should seriously consider moving
42 * this function upstream into libical.)
44 * Returns nonzero if they do overlap.
46 int ical_ctdl_is_overlap(
47 struct icaltimetype t1start,
48 struct icaltimetype t1end,
49 struct icaltimetype t2start,
50 struct icaltimetype t2end
53 if (icaltime_is_null_time(t1start)) return(0);
54 if (icaltime_is_null_time(t2start)) return(0);
56 /* First, check for all-day events */
57 if (t1start.is_date) {
58 if (!icaltime_compare_date_only(t1start, t2start, icaltimezone_get_utc_timezone())) {
61 if (!icaltime_is_null_time(t2end)) {
62 if (!icaltime_compare_date_only(t1start, t2end, icaltimezone_get_utc_timezone())) {
68 if (t2start.is_date) {
69 if (!icaltime_compare_date_only(t2start, t1start, icaltimezone_get_utc_timezone())) {
72 if (!icaltime_is_null_time(t1end)) {
73 if (!icaltime_compare_date_only(t2start, t1end, icaltimezone_get_utc_timezone())) {
79 /* lprintf (9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
80 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
81 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
84 /* Now check for overlaps using date *and* time. */
86 /* First, bail out if either event 1 or event 2 is missing end time. */
87 if (icaltime_is_null_time(t1end)) return(0);
88 if (icaltime_is_null_time(t2end)) return(0);
90 /* If event 1 ends before event 2 starts, we're in the clear. */
91 if (icaltime_compare(t1end, t2start) <= 0) return(0);
92 // lprintf(9, "first passed\n");
94 /* If event 2 ends before event 1 starts, we're also ok. */
95 if (icaltime_compare(t2end, t1start) <= 0) return(0);
96 // lprintf(9, "second passed\n");
98 /* Otherwise, they overlap. */
105 * \brief dig availability on citserver
106 * Back end function for check_attendee_availability()
107 * This one checks an individual attendee against a supplied
108 * event start and end time. All these fields have already been
110 * \param attendee_string name of the attendee
111 * \param event_start starttime of the event to check
112 * \param event_end endtime of the event to check
113 * \return The result is placed in 'annotation'.
115 void check_individual_attendee(char *attendee_string,
116 struct icaltimetype event_start,
117 struct icaltimetype event_end,
120 icalcomponent *fbc = NULL;
121 icalcomponent *fb = NULL;
122 icalproperty *thisfb = NULL;
123 struct icalperiodtype period;
126 * Set to 'unknown' right from the beginning. Unless we learn
127 * something else, that's what we'll go with.
129 strcpy(annotation, _("availability unknown"));
131 fbc = get_freebusy_for_user(attendee_string);
137 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
138 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
140 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
141 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
143 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
147 /** Iterate through all FREEBUSY's looking for conflicts. */
150 strcpy(annotation, _("free"));
152 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
154 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
157 period = icalproperty_get_freebusy(thisfb);
158 if (ical_ctdl_is_overlap(period.start, period.end,
159 event_start, event_end)) {
160 strcpy(annotation, _("BUSY"));
166 icalcomponent_free(fbc);
173 * \brief check attendees availability
174 * Check the availability of all attendees for an event (when possible)
175 * and annotate accordingly.
176 * \param vevent the event which should be compared with attendees calendar
178 void check_attendee_availability(icalcomponent *vevent) {
179 icalproperty *attendee = NULL;
180 icalproperty *dtstart_p = NULL;
181 icalproperty *dtend_p = NULL;
182 struct icaltimetype dtstart_t;
183 struct icaltimetype dtend_t;
184 char attendee_string[SIZ];
185 char annotated_attendee_string[SIZ];
186 char annotation[SIZ];
188 if (vevent == NULL) {
193 * If we're looking at a fully encapsulated VCALENDAR
194 * rather than a VEVENT component, attempt to use the first
195 * relevant VEVENT subcomponent. If there is none, the
196 * NULL returned by icalcomponent_get_first_component() will
197 * tell the next iteration of this function to create a
200 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
201 check_attendee_availability(
202 icalcomponent_get_first_component(
203 vevent, ICAL_VEVENT_COMPONENT
209 ical_dezonify(vevent); /**< Convert everything to UTC */
212 * Learn the start and end times.
214 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
215 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
217 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
218 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
221 * Iterate through attendees.
223 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
225 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
227 strcpy(attendee_string, icalproperty_get_attendee(attendee));
228 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
230 /** screen name or email address */
231 strcpy(attendee_string, &attendee_string[7]);
232 striplt(attendee_string);
234 check_individual_attendee(attendee_string,
238 /** Replace the attendee name with an annotated one. */
239 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
240 "MAILTO:%s (%s)", attendee_string, annotation);
241 icalproperty_set_attendee(attendee, annotated_attendee_string);