Calendar day view: complete in two passes (once
[citadel.git] / webcit / availability.c
1 /*
2  * $Id$
3  */
4 /**
5  *
6  * \defgroup CalendarAv  Check attendee availability for scheduling a meeting.
7  * \ingroup Calendaring
8  */
9 /*@{*/
10
11
12 #include "webcit.h"
13 #include "webserver.h"
14
15 /** only available if we have calendaring */
16 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
17
18
19
20 /**
21  * \brief verify users avaiability
22  * Utility function to fetch a VFREEBUSY type of thing for
23  * any specified user.
24  * \param who string of the user to search
25  */
26 icalcomponent *get_freebusy_for_user(char *who) {
27         char buf[SIZ];
28         char *serialized_fb = NULL;
29         icalcomponent *fb = NULL;
30
31         serv_printf("ICAL freebusy|%s", who);
32         serv_getln(buf, sizeof buf);
33         if (buf[0] == '1') {
34                 serialized_fb = read_server_text();
35         }
36
37         if (serialized_fb == NULL) {
38                 return NULL;
39         }
40         
41         fb = icalcomponent_new_from_string(serialized_fb);
42         free(serialized_fb);
43         if (fb == NULL) {
44                 return NULL;
45         }
46
47         return(fb);
48 }
49
50
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) 
53 {
54     struct icaltimetype a, b;
55
56     a = icaltime_convert_to_zone(a_in, icaltimezone_get_utc_timezone());
57     b = icaltime_convert_to_zone(b_in, icaltimezone_get_utc_timezone());
58
59     if (a.year > b.year)
60         return 1;
61     else if (a.year < b.year)
62         return -1;
63
64     else if (a.month > b.month)
65         return 1;
66     else if (a.month < b.month)
67         return -1;
68
69     else if (a.day > b.day)
70         return 1;
71     else if (a.day < b.day)
72         return -1;
73
74     /* if both are dates, we are done */
75     if (a.is_date && b.is_date)
76         return 0;
77
78     /* else, if only one is a date (and we already know the date part is equal),
79        then the other is greater */
80     else if (b.is_date)
81         return 1;
82     else if (a.is_date)
83         return -1;
84
85     else if (a.hour > b.hour)
86         return 1;
87     else if (a.hour < b.hour)
88         return -1;
89
90     else if (a.minute > b.minute)
91         return 1;
92     else if (a.minute < b.minute)
93         return -1;
94
95     else if (a.second > b.second)
96         return 1;
97     else if (a.second < b.second)
98         return -1;
99
100     return 0;
101 }
102
103
104 /**
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.
114  */
115 int ical_ctdl_is_overlap(
116                         struct icaltimetype t1start,
117                         struct icaltimetype t1end,
118                         struct icaltimetype t2start,
119                         struct icaltimetype t2end
120 ) {
121
122         if (icaltime_is_null_time(t1start)) return(0);
123         if (icaltime_is_null_time(t2start)) return(0);
124
125         /** First, check for all-day events */
126         if (t1start.is_date) {
127                 if (!icaltime_compare_date_only(t1start, t2start)) {
128                         return(1);
129                 }
130                 if (!icaltime_is_null_time(t2end)) {
131                         if (!icaltime_compare_date_only(t1start, t2end)) {
132                                 return(1);
133                         }
134                 }
135         }
136
137         if (t2start.is_date) {
138                 if (!icaltime_compare_date_only(t2start, t1start)) {
139                         return(1);
140                 }
141                 if (!icaltime_is_null_time(t1end)) {
142                         if (!icaltime_compare_date_only(t2start, t1end)) {
143                                 return(1);
144                         }
145                 }
146         }
147
148         /* lprintf (9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
149                 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
150                 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
151         */
152
153         /** Now check for overlaps using date *and* time. */
154
155         /** First, bail out if either event 1 or event 2 is missing end time. */
156         if (icaltime_is_null_time(t1end)) return(0);
157         if (icaltime_is_null_time(t2end)) return(0);
158
159         /** If event 1 ends before event 2 starts, we're in the clear. */
160         if (webcit_icaltime_compare(t1end, t2start) <= 0) return(0);
161         // lprintf(9, "first passed\n");
162
163         /** If event 2 ends before event 1 starts, we're also ok. */
164         if (webcit_icaltime_compare(t2end, t1start) <= 0) return(0);
165         // lprintf(9, "second passed\n");
166
167         /** Otherwise, they overlap. */
168         return(1);
169 }
170
171
172
173 /*
174  * \brief dig availability on citserver
175  * Back end function for check_attendee_availability()
176  * This one checks an individual attendee against a supplied
177  * event start and end time.  All these fields have already been
178  * broken out.  
179  * \param attendee_string name of the attendee
180  * \param event_start starttime of the event to check
181  * \param event_end endtime of the event to check
182  * \return The result is placed in 'annotation'.
183  */
184 void check_individual_attendee(char *attendee_string,
185                                 struct icaltimetype event_start,
186                                 struct icaltimetype event_end,
187                                 char *annotation) {
188
189         icalcomponent *fbc = NULL;
190         icalcomponent *fb = NULL;
191         icalproperty *thisfb = NULL;
192         struct icalperiodtype period;
193
194         /**
195          * Set to 'unknown' right from the beginning.  Unless we learn
196          * something else, that's what we'll go with.
197          */
198         strcpy(annotation, _("availability unknown"));
199
200         fbc = get_freebusy_for_user(attendee_string);
201         if (fbc == NULL) {
202                 return;
203         }
204
205         /**
206          * Make sure we're looking at a VFREEBUSY by itself.  What we're probably
207          * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
208          */
209         if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
210                 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
211         }
212         else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
213                 fb = fbc;
214         }
215
216         /** Iterate through all FREEBUSY's looking for conflicts. */
217         if (fb != NULL) {
218
219                 strcpy(annotation, _("free"));
220
221                 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
222                     thisfb != NULL;
223                     thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
224
225                         /** Do the check */
226                         period = icalproperty_get_freebusy(thisfb);
227                         if (ical_ctdl_is_overlap(period.start, period.end,
228                            event_start, event_end)) {
229                                 strcpy(annotation, _("BUSY"));
230                         }
231
232                 }
233         }
234
235         icalcomponent_free(fbc);
236 }
237
238
239
240
241 /**
242  * \brief check attendees availability
243  * Check the availability of all attendees for an event (when possible)
244  * and annotate accordingly.
245  * \param vevent the event which should be compared with attendees calendar
246  */
247 void check_attendee_availability(icalcomponent *vevent) {
248         icalproperty *attendee = NULL;
249         icalproperty *dtstart_p = NULL;
250         icalproperty *dtend_p = NULL;
251         struct icaltimetype dtstart_t;
252         struct icaltimetype dtend_t;
253         char attendee_string[SIZ];
254         char annotated_attendee_string[SIZ];
255         char annotation[SIZ];
256
257         if (vevent == NULL) {
258                 return;
259         }
260
261         /**
262          * If we're looking at a fully encapsulated VCALENDAR
263          * rather than a VEVENT component, attempt to use the first
264          * relevant VEVENT subcomponent.  If there is none, the
265          * NULL returned by icalcomponent_get_first_component() will
266          * tell the next iteration of this function to create a
267          * new one.
268          */
269         if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
270                 check_attendee_availability(
271                         icalcomponent_get_first_component(
272                                 vevent, ICAL_VEVENT_COMPONENT
273                         )
274                 );
275                 return;
276         }
277
278         ical_dezonify(vevent);          /**< Convert everything to UTC */
279
280         /**
281          * Learn the start and end times.
282          */
283         dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
284         if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
285
286         dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
287         if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
288
289         /**
290          * Iterate through attendees.
291          */
292         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
293             attendee != NULL;
294             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
295
296                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
297                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
298
299                         /** screen name or email address */
300                         strcpy(attendee_string, &attendee_string[7]);
301                         striplt(attendee_string);
302
303                         check_individual_attendee(attendee_string,
304                                                 dtstart_t, dtend_t,
305                                                 annotation);
306
307                         /** Replace the attendee name with an annotated one. */
308                         snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
309                                 "MAILTO:%s (%s)", attendee_string, annotation);
310                         icalproperty_set_attendee(attendee, annotated_attendee_string);
311
312                 }
313         }
314
315 }
316
317
318 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */
319
320 /** @} */