* availability.c: brought over ical_ctdl_is_overlap() from Citadel. Used
[citadel.git] / webcit / availability.c
1 /*
2  * $Id$
3  *
4  * Check attendee availability for scheduling a meeting.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30
31 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
32
33
34
35 /*
36  * Utility function to fetch a VFREEBUSY type of thing for
37  * any specified user.
38  */
39 icalcomponent *get_freebusy_for_user(char *who) {
40         char buf[SIZ];
41         char *serialized_fb = NULL;
42         icalcomponent *fb = NULL;
43
44         serv_printf("ICAL freebusy|%s", who);
45         serv_gets(buf);
46         if (buf[0] == '1') {
47                 serialized_fb = read_server_text();
48         }
49
50         if (serialized_fb == NULL) {
51                 return NULL;
52         }
53         
54         fb = icalcomponent_new_from_string(serialized_fb);
55         free(serialized_fb);
56         if (fb == NULL) {
57                 return NULL;
58         }
59
60         return fb;
61 }
62
63
64
65
66
67
68
69 /*
70  * Check to see if two events overlap.  Returns nonzero if they do.
71  * (This function is used in both Citadel and WebCit.  If you change it in
72  * one place, change it in the other.  Better yet, put it in a library.)
73  */
74 int ical_ctdl_is_overlap(
75                         struct icaltimetype t1start,
76                         struct icaltimetype t1end,
77                         struct icaltimetype t2start,
78                         struct icaltimetype t2end
79 ) {
80
81         if (icaltime_is_null_time(t1start)) return(0);
82         if (icaltime_is_null_time(t2start)) return(0);
83
84         /* First, check for all-day events */
85         if (t1start.is_date) {
86                 if (!icaltime_compare_date_only(t1start, t2start)) {
87                         return(1);
88                 }
89                 if (!icaltime_is_null_time(t2end)) {
90                         if (!icaltime_compare_date_only(t1start, t2end)) {
91                                 return(1);
92                         }
93                 }
94         }
95
96         if (t2start.is_date) {
97                 if (!icaltime_compare_date_only(t2start, t1start)) {
98                         return(1);
99                 }
100                 if (!icaltime_is_null_time(t1end)) {
101                         if (!icaltime_compare_date_only(t2start, t1end)) {
102                                 return(1);
103                         }
104                 }
105         }
106
107         /* Now check for overlaps using date *and* time. */
108
109         /* First, bail out if either event 1 or event 2 is missing end time. */
110         if (icaltime_is_null_time(t1end)) return(0);
111         if (icaltime_is_null_time(t2end)) return(0);
112
113         /* If event 1 ends before event 2 starts, we're in the clear. */
114         if (icaltime_compare(t1end, t2start) <= 0) return(0);
115
116         /* If event 2 ends before event 1 starts, we're also ok. */
117         if (icaltime_compare(t2end, t1start) <= 0) return(0);
118
119         /* Otherwise, they overlap. */
120         return(1);
121 }
122
123
124
125
126
127
128
129 /*
130  * Back end function for check_attendee_availability()
131  * This one checks an individual attendee against a supplied
132  * event start and end time.  All these fields have already been
133  * broken out.  The result is placed in 'annotation'.
134  */
135 void check_individual_attendee(char *attendee_string,
136                                 struct icaltimetype event_start,
137                                 struct icaltimetype event_end,
138                                 char *annotation) {
139
140         icalcomponent *fbc = NULL;
141         icalcomponent *fb = NULL;
142         icalproperty *thisfb = NULL;
143         struct icalperiodtype period;
144
145         /* Set to 'unknown' right from the beginning.  Unless we learn
146          * something else, that's what we'll go with.
147          */
148         strcpy(annotation, "availability unknown");
149
150         fbc = get_freebusy_for_user(attendee_string);
151         if (fbc == NULL) {
152                 return;
153         }
154
155         /* Make sure we're looking at a VFREEBUSY by itself.  What we're probably
156          * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
157          */
158         if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
159                 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
160         }
161         else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
162                 fb = fbc;
163         }
164
165         /* Iterate through all FREEBUSY's looking for conflicts. */
166         if (fb != NULL) {
167
168                 strcpy(annotation, "free");
169
170                 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
171                     thisfb != NULL;
172                     thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
173
174                         /* Do the check */
175                         period = icalproperty_get_freebusy(thisfb);
176                         if (ical_ctdl_is_overlap(period.start, period.end,
177                            event_start, event_end)) {
178                                 strcpy(annotation, "BUSY");
179                         }
180
181                 }
182         }
183
184         icalcomponent_free(fbc);
185 }
186
187
188
189
190 /*
191  * Check the availability of all attendees for an event (when possible)
192  * and annotate accordingly.
193  */
194 void check_attendee_availability(icalcomponent *vevent) {
195         icalproperty *attendee = NULL;
196         icalproperty *dtstart_p = NULL;
197         icalproperty *dtend_p = NULL;
198         struct icaltimetype dtstart_t;
199         struct icaltimetype dtend_t;
200         char attendee_string[SIZ];
201         char annotated_attendee_string[SIZ];
202         char annotation[SIZ];
203
204         if (vevent == NULL) {
205                 return;
206         }
207
208         /* If we're looking at a fully encapsulated VCALENDAR
209          * rather than a VEVENT component, attempt to use the first
210          * relevant VEVENT subcomponent.  If there is none, the
211          * NULL returned by icalcomponent_get_first_component() will
212          * tell the next iteration of this function to create a
213          * new one.
214          */
215         if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
216                 check_attendee_availability(
217                         icalcomponent_get_first_component(
218                                 vevent, ICAL_VEVENT_COMPONENT
219                         )
220                 );
221                 return;
222         }
223
224         ical_dezonify(vevent);          /* Convert everything to UTC */
225
226         /*
227          * Learn the start and end times.
228          */
229         dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
230         if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
231
232         dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
233         if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
234
235         /*
236          * Iterate through attendees.
237          */
238         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
239             attendee != NULL;
240             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
241
242                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
243                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
244
245                         /* screen name or email address */
246                         strcpy(attendee_string, &attendee_string[7]);
247                         striplt(attendee_string);
248
249                         check_individual_attendee(attendee_string,
250                                                 dtstart_t, dtend_t,
251                                                 annotation);
252
253                         /* Replace the attendee name with an annotated one. */
254                         snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
255                                 "MAILTO:%s (%s)", attendee_string, annotation);
256                         icalproperty_set_attendee(attendee, annotated_attendee_string);
257
258                 }
259         }
260
261 }
262
263
264 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */