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