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