f2b4cdbe766a65875f52931c9fa9f6df1596865d
[citadel.git] / webcit / availability.c
1 /*
2  * $Id$
3  *
4  */
5
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10 /*
11  * Utility function to fetch a VFREEBUSY type of thing for any specified user.
12  */
13 icalcomponent *get_freebusy_for_user(char *who) {
14         char buf[SIZ];
15         char *serialized_fb = NULL;
16         icalcomponent *fb = NULL;
17
18         serv_printf("ICAL freebusy|%s", who);
19         serv_getln(buf, sizeof buf);
20         if (buf[0] == '1') {
21                 serialized_fb = read_server_text();
22         }
23
24         if (serialized_fb == NULL) {
25                 return NULL;
26         }
27         
28         fb = icalcomponent_new_from_string(serialized_fb);
29         free(serialized_fb);
30         if (fb == NULL) {
31                 return NULL;
32         }
33
34         return(fb);
35 }
36
37
38 /*
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.)
43  *
44  * Returns nonzero if they do overlap.
45  */
46 int ical_ctdl_is_overlap(
47                         struct icaltimetype t1start,
48                         struct icaltimetype t1end,
49                         struct icaltimetype t2start,
50                         struct icaltimetype t2end
51 ) {
52
53         if (icaltime_is_null_time(t1start)) return(0);
54         if (icaltime_is_null_time(t2start)) return(0);
55
56         /* First, check for all-day events */
57         if (t1start.is_date) {
58                 if (!icaltime_compare_date_only(t1start, t2start)) {
59                         return(1);
60                 }
61                 if (!icaltime_is_null_time(t2end)) {
62                         if (!icaltime_compare_date_only(t1start, t2end)) {
63                                 return(1);
64                         }
65                 }
66         }
67
68         if (t2start.is_date) {
69                 if (!icaltime_compare_date_only(t2start, t1start)) {
70                         return(1);
71                 }
72                 if (!icaltime_is_null_time(t1end)) {
73                         if (!icaltime_compare_date_only(t2start, t1end)) {
74                                 return(1);
75                         }
76                 }
77         }
78
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);
82         */
83
84         /* Now check for overlaps using date *and* time. */
85
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);
89
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");
93
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");
97
98         /* Otherwise, they overlap. */
99         return(1);
100 }
101
102
103
104 /*
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
109  * broken out.  
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'.
114  */
115 void check_individual_attendee(char *attendee_string,
116                                 struct icaltimetype event_start,
117                                 struct icaltimetype event_end,
118                                 char *annotation) {
119
120         icalcomponent *fbc = NULL;
121         icalcomponent *fb = NULL;
122         icalproperty *thisfb = NULL;
123         struct icalperiodtype period;
124
125         /**
126          * Set to 'unknown' right from the beginning.  Unless we learn
127          * something else, that's what we'll go with.
128          */
129         strcpy(annotation, _("availability unknown"));
130
131         fbc = get_freebusy_for_user(attendee_string);
132         if (fbc == NULL) {
133                 return;
134         }
135
136         /**
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.
139          */
140         if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
141                 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
142         }
143         else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
144                 fb = fbc;
145         }
146
147         /** Iterate through all FREEBUSY's looking for conflicts. */
148         if (fb != NULL) {
149
150                 strcpy(annotation, _("free"));
151
152                 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
153                     thisfb != NULL;
154                     thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
155
156                         /** Do the check */
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"));
161                         }
162
163                 }
164         }
165
166         icalcomponent_free(fbc);
167 }
168
169
170
171
172 /**
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
177  */
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];
187
188         if (vevent == NULL) {
189                 return;
190         }
191
192         /**
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
198          * new one.
199          */
200         if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
201                 check_attendee_availability(
202                         icalcomponent_get_first_component(
203                                 vevent, ICAL_VEVENT_COMPONENT
204                         )
205                 );
206                 return;
207         }
208
209         ical_dezonify(vevent);          /**< Convert everything to UTC */
210
211         /**
212          * Learn the start and end times.
213          */
214         dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
215         if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
216
217         dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
218         if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
219
220         /**
221          * Iterate through attendees.
222          */
223         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
224             attendee != NULL;
225             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
226
227                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
228                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
229
230                         /** screen name or email address */
231                         strcpy(attendee_string, &attendee_string[7]);
232                         striplt(attendee_string);
233
234                         check_individual_attendee(attendee_string,
235                                                 dtstart_t, dtend_t,
236                                                 annotation);
237
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);
242
243                 }
244         }
245
246 }
247