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