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