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