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